1

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);
}
Nirav D
  • 71,513
  • 12
  • 161
  • 183
AMI amitekh
  • 198
  • 3
  • 16

3 Answers3

1

Just subclass UIButton and add properties in that class which you want to pass as parameter.

then you can assess it by instance of that button. for example,

 #import <UIKit/UIKit.h>

@interface CustomFileCapturebutton : UIButton

@property int maxNumberOfFilesAllow;
@property NSString *fileType;

@end

then create instance of CustomFileCapturebutton and create action something like,

-(void)captureClick : (CustomFileCapturebutton*)sender{

    // you can use your properties here like



   NSLog (@"%@",sender.fileType);

 }

you can set that properties at that time when you addtarget on button like,

  CustomFileCapturebutton *btn = [[CustomFileCapturebutton alloc]init];

  btn.frame = yourFrame;

   [btn addTarget:self action:@selector(captureClick:) forControlEvents:UIControlEventTouchUpInside];

  btn.fileType = @"png";   // set properties here
Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
1

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);
     }
}
Nirav D
  • 71,513
  • 12
  • 161
  • 183
1

Try accessibilityIdentifier for pass second argument to button

button.accessibilityIdentifier = [NSString stringWithFormat:@"%d",indexPath.section)];
Sandeep Kumar
  • 328
  • 1
  • 8