action:@selector(showAlert:) how to pass parameter in this showAlert method?

12,665

Solution 1

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.

Solution 2

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
}

Solution 3

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.

Share:
12,665
jszumski
Author by

jszumski

iOS engineer & consultant. Co-author of Professional iOS Network Programming: Connecting the Enterprise to the iPhone and iPad.

Updated on June 09, 2022

Comments

  • jszumski
    jszumski almost 2 years

    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:)?

  • manuelBetancurt
    manuelBetancurt almost 12 years
    Excellent!!, nice casting!, this answer is very informative!, thanks!
  • Joshua Dance
    Joshua Dance over 10 years
    You don't have to use IBAction. See other answers.