AMI amitekh AMI amitekh - 1 month ago 10
Objective-C Question

How to pass two arguments to a UIButtons target method in iOS?

In my code I've to pass two arguments to targetMethod

printMethod
, I can pass the button.tag as one argument and how to pass the other argument?

Please Give an Example.

My code:

button.tag = indexPath.row;
secondArgument = indexPath.section;
[button addTarget:self action:@selector(printMethod:) forControlEvents:UIControlEventTouchUpInside];

-(IBAction)printMethod:(UIButton*)sender{
NSLog(@"%d%d",sender.tag,//SecondArgument);
}

Answer

If you want the indexPath on button action then try something like this.

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

     CGPoint point = [sender.superview convertPoint:sender.center toView:self.tableView];
     NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];
     if (indexPath) {
          NSLog(@"Section - %ld Row - %ld",deleteIndexPath.section, deleteIndexPath.row);
     }
}