English 中文(简体)
在Runtime(Similar to twitter Follow/Un follow)上,在同一纽扣地进行不同的行动?
原标题:Performing different actions on same button click during Runtime (Similar to twitter Follow/UnFollow)?

我有以下方案结构:

-(UITableViewCell *)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {

       ...

       ...


       if(cell == nil)
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];

            ...

            ...


            if(condition)
                {   
                    do something;
                }

            else
                {

                    if(condition)
                         {  
                            unFollowButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                            unFollowButton.frame = CGRectMake(180, 10, 120, 30);

                            unFollowButton.titleLabel.font = [UIFont systemFontOfSize:12];
                            unFollowButton.tag = indexPath.row;
                            unFollowButton.titleLabel.textColor = [UIColor blackColor];
                            [unFollowButton addTarget:self action:@selector(buttonClicked2:) forControlEvents:UIControlEventTouchUpInside];
                            NSLog(@"fCheckRowCheck Value in if Condition %@",fCheckRowCheck);
                            [unFollowButton setTitle:@"UnFollow" forState:UIControlStateNormal];
                            [cell.contentView addSubview:unFollowButton];
                            buttonValue = 0;
                            NSLog(@"buttonValue %d", buttonValue);
                        }

                    else 
                        {

                            followButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                            followButton.frame = CGRectMake(180, 10, 120, 30);
                            followButton.titleLabel.font = [UIFont systemFontOfSize:12];
                            followButton.tag = indexPath.row;
                            followButton.titleLabel.textColor = [UIColor blackColor];
                            [followButton setTitle:@"Follow" forState:UIControlStateNormal];
                            [followButton addTarget:self action:@selector(buttonClicked1:) forControlEvents:UIControlEventTouchUpInside];
                            [cell.contentView addSubview:followButton];
                            buttonValue = 1;
                            NSLog(@"buttonValue %d", buttonValue);
                        }   
                }
        }


    NSUInteger row = [indexPath row];
    cell.textLabel.text = [self.infos objectAtIndex:row];
    return cell;

}




- (IBAction)buttonClicked2:(UIButton *)sender
    {
       NSLog(@"BUTTON_CLICKED");

       NSIndexPath *indexPath = [folksFolksTable indexPathForCell:(UITableViewCell*)  [[sender superview]superview]];
       NSLog(@"[sender tag] is %d", [sender tag]);

       ....

       ....

       ....

       //Set up URLConnection tp send information on button click
       NSMutableString *postString = [NSMutableString stringWithString:kUnFollowURL];
    [postString appendString: [NSString stringWithFormat:@"?%@=%@", kId, [user objectForKey:@"id"] ]];
    [postString appendString: [NSString stringWithFormat:@"&%@=%@", kfId, [fId objectForKey:@"fID"] ]];

    [postString setString: [postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSLog(@"post string = %@", postString);

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]];

    [request setHTTPMethod:@"POST"];

    followConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
    NSLog(@"postconnection: %@", followConnection);

    //Get Response from server

    NSData *dataURL =  [NSData dataWithContentsOfURL: [ NSURL URLWithString: postString ]];    
    NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];

    NSLog(@"serverOutput = %@", serverOutput);

    //Change the button lable from unfollow to follow

    [sender setTitle:@"Follow" forState:UIControlStateNormal]; 




     if([sender tag]==indexPath.row)
        {
            textField = (UITextField*)[cell viewWithTag:[sender tag]];
            NSLog(@"txtF is %@",textField);        
            textField.hidden=NO;
        }


}

- (IBAction)buttonClicked1:(UIButton *)sender
      {

          similar to buttonClicked 2

      }

我需要的是,在标签在运行期间发生变化之后,纽伦应当采取相应的行动。

例如,如果我点击纽州[纽顿标签],那么我就在那个时候就看到了(正在完全是科索沃)的标签。 现在,当我再次用“黄色”标签点击同一纽州时,它就成为例外。

How to go about it. Please help me figure it out ?

最佳回答

EDIT: 解决。 此前,我增加了以下法典。

[送交:@>

 [sender addTarget:self action:@selector(buttonClicked2:) forControlEvents:UIControlEventTouchUpInside];

cause of exception was i added colon (:) twice after @selector(buttonClicked2) whereas i need only one colon.

I hope this question serves as reference to other users as well.

问题回答

I m new toOS but:

  1. Set a property that has the button state, or extend the button (some people don t recommend)
  2. When it changes state, change the color and the label.

这样做。 或许其他用户有更好的解决办法。

乘客。





相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

Iphone NSTimer Issue

Hi I am new to objective c. I am trying to make an app for iphone. I have a button on my view, and the click on which the function playSound is called. This is working properly. It does plays the ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Xcode open two editor windows with same file

Is it possible to open the same file in two separate windows in Xcode. I can open a file in one window and the same file in the main Xcode editor window, but I wanted two separate fulltime editor ...

Forcing code signing refresh in Xcode

In our environment, we share resources across multiple projects and platforms. When building for iPhone, only a subset of those resources are needed. Since that subset is still considerable, we have ...

热门标签