1

在我的代码中,我必须将两个参数传递给 targetMethod printMethod,我可以将 button.tag 作为一个参数传递,以及如何传递另一个参数?

请举个例子。

我的代码:

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

3 回答 3

1

只需子类UIButton化并在要作为参数传递的该类中添加属性。

然后您可以通过该按钮的实例对其进行评估。例如,

 #import <UIKit/UIKit.h>

@interface CustomFileCapturebutton : UIButton

@property int maxNumberOfFilesAllow;
@property NSString *fileType;

@end

然后创建实例CustomFileCapturebutton并创建类似的操作,

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

    // you can use your properties here like



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

 }

您可以addtarget在按钮上设置该属性,例如,

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

  btn.frame = yourFrame;

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

  btn.fileType = @"png";   // set properties here
于 2016-10-13T10:25:30.573 回答
1

如果您想要indexPath打开按钮操作,请尝试这样的操作。

-(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);
     }
}
于 2016-10-13T10:25:32.963 回答
1

尝试accessibilityIdentifier将第二个参数传递给按钮

button.accessibilityIdentifier = [NSString stringWithFormat:@"%d",indexPath.section)];
于 2016-10-13T10:29:02.823 回答