在向UIImageView添加UITapGestureRecognizer时执行错误访问

时间:2014-09-02 09:05:11

标签: ios objective-c uiimageview uigesturerecognizer uitapgesturerecognizer

我正在尝试向UIImageView添加手势识别器,但访问不当。

[UISwipeGestureRecognizer openPhotoDetail:]: unrecognized selector sent to instance 0x17e589a0
2014-09-02 12:01:32.569 FotoTR[1949:669649] *** Terminating app due to uncaught exception   'NSInvalidArgumentException', reason: '-[UISwipeGestureRecognizer openPhotoDetail:]: unrecognized    selector sent to instance 0x17e589a0'
*** First throw call stack:

这是我的代码;

  mainView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 408)];
    [self.view addSubview:mainView];


    firstVerticalImageView = [[STGImageView alloc]initWithFrame:CGRectMake(4, 4, 153, 197)];
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openPhotoDetail:)];
    [firstVerticalImageView addGestureRecognizer:singleTap];
    [firstVerticalImageView setUserInteractionEnabled:YES];
    [mainView addSubview:firstVerticalImageView];


    [mainView bringSubviewToFront:firstVerticalImageView];

这有什么问题?

我的openPhotoDetail方法;

-(void)openPhotoDetail:(id)sender
{

PhotoDetailViewController *photoDetailViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"photoDetailViewController"];

[self.navigationController pushViewController:photoDetailViewController animated:YES];    
}

3 个答案:

答案 0 :(得分:0)

试试这段代码

mainView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 408)];
    [self.view addSubview:mainView];


    firstVerticalImageView = [[STGImageView alloc]initWithFrame:CGRectMake(4, 4, 153, 197)];
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openPhotoDetail:)];
    [firstVerticalImageView addGestureRecognizer:singleTap];
    firstVerticalImageView.tag=1;
    [firstVerticalImageView setUserInteractionEnabled:YES];
    [mainView addSubview:firstVerticalImageView];

[mainView bringSubviewToFront:firstVerticalImageView];


-(void)openPhotoDetail:(UIGestureRecognizer *)recognizer

    {

        // if you have tag value of your uiimageview ,you can get here like this try this code
    UIImageView *theTappedImageView = (UIImageView *)recognizer.view;
    int yourtag =theTappedImageView.tag ;
        NSLog(@"clicked");
 NSLog(@"yourtag item==%d", yourtag);


    }

答案 1 :(得分:0)

请参阅以下代码以进行点击手势。

    UITapGestureRecognizer *tapGR =
    [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openPhotoDetail:)];
    [firstVerticalImageView addGestureRecognizer: tapGR];

    // Selector is something like this

-(void) openPhotoDetail:(UITapGestureRecognizer *)gestureRecognizer
{
    // Label tapped, your turn ...
    // Use gestureRecognizer parameter if you need the view tapped
    UIImageView *img = gestureRecognizer.View;
}

答案 2 :(得分:-1)

你的方法错了,它应该是这样的 -

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

   PhotoDetailViewController *photoDetailViewController =
  [self.storyboard
   instantiateViewControllerWithIdentifier:@"photoDetailViewController"];

   [self.navigationController
   pushViewController:photoDetailViewController animated:YES];  

   }