滑动时无法识别的选择器

时间:2012-03-20 16:12:44

标签: objective-c ios cocoa-touch unrecognized-selector

我需要抓住特殊区域的滑动动作。但是调试器告诉unrecognized selector sent to instance

- (void)viewDidLoad
{
    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc]
          initWithTarget:self.viewName action:@selector(didSwipe:)];
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:swipeLeft];
}

-(void)didSwipe:(UISwipeGestureRecognizer*)swipe{
   NSLog(@"swiped left");
}

怎么了?

2 个答案:

答案 0 :(得分:6)

你的手势识别器有一个参数,所以它应该是@selector(didSwipe:)(注意冒号)。

答案 1 :(得分:3)

您可能希望目标为self而不是self.imageName


修改以回应您的评论。

UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc]
      initWithTarget:self action:@selector(didSwipe:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[self.viewName addGestureRecognizer:swipeLeft];

您希望target成为该方法所在的位置。在这种情况下应该是self。您希望将手势识别器添加到应该触摸的视图self.viewName