Search code examples
iosobjective-cuibuttonaddtarget

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);
}

Solution

  • 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);
         }
    }