【问题标题】:I don't get why this @selector isn't working我不明白为什么这个@selector 不起作用
【发布时间】:2014-02-10 09:51:42
【问题描述】:

我正在创建几个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 是在MainStoryboard 中创建的UIView

我已经通过使用普通的 @selector() 检查了它是否是 NSSelectorFromString(),这不起作用,但这不是原因。

如果我将[imgView addGestureRecognizer:... 更改为[freeView addGestureRecognizer:...],它确实有效

我似乎不明白为什么会这样。

【问题讨论】:

  • 你需要设置 userInteractionEnabled=YES 因为它默认为 NO。所以触摸交互会被 imageView 接收到
  • 看看你的 iniiWithTarget: 参数。这就是您尝试调用其选择器的对象。确保它与实现您所追求的选择器的那个匹配。

标签: ios uiview action selector uigesturerecognizer


【解决方案1】:

试试这个

imgView.userInteractionEnabled = YES ;

UIImageView 中的userInteractionEnabled 默认设置为 NO。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-15
      • 1970-01-01
      • 1970-01-01
      • 2012-05-25
      • 2014-04-20
      • 1970-01-01
      • 2010-12-14
      • 2021-07-10
      相关资源
      最近更新 更多