我正在创建多个UIView
,我添加了一个UITapGestureRecognizer
,其中action
可以调用viewController
中的方法,但似乎没有工作。
-(void)setupFreeMoneyView {
NSArray *array = @[@"Facebook", @"Twitter", @"SMS", @"Email"];
int index = 0;
for (NSString *str in array) {
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100*index++, 0, 100, 100)];
[imgView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@Logo", str]]];
[imgView addGestureRecognizer:[[UITapGestureRecognizer alloc]
initWithTarget:self
action:NSSelectorFromString([NSString stringWithFormat:@"postTo%@", str])]];
[freeView addSubview:imgView];
}
}
freeView
是UIView
中创建的MainStoryboard
。
我通过使用正常的NSSelectorFromString()
检查了@selector()
是否无效,但不是原因。
如果我将行[imgView addGestureRecognizer:...
更改为[freeView addGestureRecognizer:...]
我似乎不明白为什么会这样。
答案 0 :(得分:1)
试试这个
imgView.userInteractionEnabled = YES ;
userInteractionEnabled
的{{1}}设置为NO。
答案 1 :(得分:1)
也许你需要为UIImageView设置userInteractionEnabled = YES