【问题标题】:Adding a gesture in code gives NSInvalidArgumentException在代码中添加手势会产生 NSInvalidArgumentException
【发布时间】:2013-11-16 18:29:30
【问题描述】:

在开发演示应用时,我在 XCode 中添加了一些手势,它们运行良好。但是,在代码中添加手势会在运行时给出 NSInvalidArgumentException,此时应该调用手势。我试图将此手势添加到 ImageView,但后来我也尝试使用 self.view。一切都无济于事:(

这可能是一些与内存相关的问题,但我无法解决它。任何帮助将不胜感激。

附:为 imageView 启用了用户交互 - 当在 XCode 中添加手势时,代码运行良好。


这是调试器显示的内容:

'NSInvalidArgumentException',原因:'-[UIView handleToyImageTap:]:无法识别的选择器发送到实例。

代码如下:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    previousRotation = 4.0;
    [self.toyImageTappable addGestureRecognizer: [[UITapGestureRecognizer alloc] initWithTarget:self.toyImageTappable action:@selector(handleToyImageTap:)]];

}

- (void) handleToyImageTap:(UITapGestureRecognizer *)sender {

    //Rotate image by 45 degree
    self.toyImageTappable.transform = CGAffineTransformMakeRotation(M_PI/previousRotation);

    previousRotation = previousRotation + 4.0;
    if (previousRotation == 16.0)
        previousRotation = 4.0;

}

【问题讨论】:

    标签: ios objective-c ios7 gesture-recognition runtimeexception


    【解决方案1】:
    [self.toyImageTappable addGestureRecognizer: [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleToyImageTap:)]];
    

    【讨论】:

      【解决方案2】:

      target 应该是你观察要调用的动作方法的对象,在本例中为self

      [self.toyImageTappable addGestureRecognizer: [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleToyImageTap:)]];
      

      【讨论】:

      • 我也试过了 :( 虽然在这种情况下,我希望我的目标是 imageView 本身。
      • 好的,我试过这个:目标自我,如你所说,并将识别器添加到 UIImageView。它为我完成了这项工作:) 感谢您的帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多