2014-02-10 74 views
0

我创建几个UIView的,我加UITapGestureRecognizeraction这就是想打电话给在viewController的方法,但它似乎并没有工作。我不明白为什么这个@selector不工作

-(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是在MainStoryboard创建UIView

我检查了是否NSSelectorFromString()通过使用正常的@selector()那不工作,但它不是原因。

如果我改变行[imgView addGestureRecognizer:...[freeView addGestureRecognizer:...]

我似乎就是不明白为什么会这样,它工作。

+0

u需要设置userInteractionEnabled = YES,因为它默认为NO。所以触摸交互将通过imageView – CoolMonster

+0

收到你的iniiWithTarget:参数。这是您试图呼叫的选择对象。确保它与实现您之后选择器的那个匹配。 – verec

回答

1

试试这个

imgView.userInteractionEnabled = YES ;

UIImageViewuserInteractionEnabled被默认设置为NO。

+0

哦,是的,我忘了...谢谢。 – Arbitur

1

也许你需要设置userInteractionEnabled = YES为的UIImageView

相关问题