如何将两个参数传递给 iOS 中的 UIButtons 目标方法?
How to pass two arguments to a UIButtons target method in iOS?
在我的代码中,我必须将两个参数传递给 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);
}
只需 subclass UIButton
并在 class 中添加您想要作为参数传递的属性。
然后您可以通过该按钮的实例对其进行评估。例如,
#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
如果您想要 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);
}
}
尝试 accessibilityIdentifier 将第二个参数传递给按钮
button.accessibilityIdentifier = [NSString stringWithFormat:@"%d",indexPath.section)];
在我的代码中,我必须将两个参数传递给 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);
}
只需 subclass UIButton
并在 class 中添加您想要作为参数传递的属性。
然后您可以通过该按钮的实例对其进行评估。例如,
#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
如果您想要 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);
}
}
尝试 accessibilityIdentifier 将第二个参数传递给按钮
button.accessibilityIdentifier = [NSString stringWithFormat:@"%d",indexPath.section)];