如果我有
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(somefunction:)];
-(void)somefunction :(id) sender{}
将变量传递给某个函数的正确语法是什么?我试过了(但它不起作用)
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(somefunction: somensstring:)];
-(void)somefunction: (NSString *) somestuff :(id) sender{}
答案 0 :(得分:2)
//添加点按手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(HandleTap:)];
//在TableView中获取TouchLocation
-(void)HandleTap:(UITapGestureRecognizer*)tap
{
CGPoint point = [tap locationInView:theTableView];
NSIndexPath *theIndexPath = [theTableView indexPathForRowAtPoint:point];
}
你可以根据tableView From Array的indexPath获得你想要的任何信息;
答案 1 :(得分:1)
您应该以这种方式定义somefunction
方法,
-(void)somefunction:(UIGestureRecognizer *) sender {};
-(void)somefunction:(UIGestureRecognizer *) sender {
UIView *view = (UIView *)sender.view;
//1.here you can take any view, UILabel, UIImageView or anything on which you've
//added tap gesture
//2.now, distinguish view by tag
if(view.tag == someTag1) {
//create a NSString to use when tap
}
else if(view.tag == someTag2) {
//do something else
}
};
UILabel
上设置了点按手势,则创建UILabel
的子类,然后在其上添加手势。这里是UILabel
子类的例子,
@interface MyCustomLabel : UILabel
@property (nonatomic, strong) NSString *someString;
@end;
现在您可以使用此自定义标签,
#import "MyCustomLabel.h"
MyCustomLable *lbl = [[MyCustomLable alloc] init];
lbl.frame = CGRectMake(0,0,200,50);
//add gesture on label
lbl.someString = @"Some text";
[self.view addSubview:lbl];
现在上面的方法几乎没有变化,
-(void)somefunction:(UIGestureRecognizer *) sender {
MyCustomLabel *lbl = (MyCustomLabel *)sender.view;
NSLog(@"My String %@",lbl.someString);
};