Question

I am adding custom button to my UITableViewCell. In the action of that button I want to call showAlert: function and want to pass cell label in the method.

How can I pass a parameter in this showAlert method: action:@selector(showAlert:)?

Was it helpful?

Solution

That's not possible. You have to create a method conforming to IBAction

- (IBAction)buttonXYClicked:(id)sender;

In this method you can create and call the UIAlertView. Don't forget to connect the button with the method in Interface Builder.

If you like to distinguish between multiple Buttons (e.g. you have one in each table cell) you can set the tag property of the button. And then check sender.tag from which button the click comes.

OTHER TIPS

If you are using Button in Tableviewcell then u have to add tag value to each cell's button and set the method addTarget with id as a parameter.

Sample Code:

You have to Type Below code in cellForRowAtIndexPath method.

{

     // Set tag to each button
        cell.btn1.tag = indexPath.row; 
        [cell.btn1 setTitle:@"Select" forState:UIControlStateNormal];  // Set title 

     // Add Target with passing id like this
        [cell.btn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];    


     return cell;

}

-(void)btnClick:(id)sender
{

    UIButton* btn = (UIButton *) sender;

     // here btn is the selected button...
        NSLog(@"Button %d is selected",btn.tag); 


    // Show appropriate alert by tag values
}

Jay's answer is great but if you have multiple sections it won't work since the indexRow is local to a section.

Another method, if you are using buttons in a TableView that has multiple sections, is to pass the touch events.

Where you declare your button in a lazy loader:

- (UIButton *)awesomeButton
{
    if(_awesomeButton == nil)
    {
        _awesomeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [_awesomeButton addTarget:self.drugViewController action:@selector(buttonPressed:event:) forControlEvents:UIControlEventTouchUpInside];
    }

    return _awesomeButton;
}

The key here is to chain your events onto the selector method. You can't pass your own parameters, but you can pass the events.

The function the button is hooked to:

- (void)buttonPressed:(id)sender event:(id)event
{
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];

    CGPoint currentTouchPosition = [touch locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];

    NSLog(@"Button %d was pressed in section %d",indexPath.row, indexPath.section);
}

The key here is the function indexPathForRowAtPoint. This is a nifty function in UITableView that will give you the indexPath at any point. Also important is the function locationInView because you need the touch in the context of the tableView so it can pinpoint the specific indexPath.

This will allow you to know which button it was, in a table with multiple sections.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top