在将 UITapGestureRecognizer 添加到 UIImageView 时进行错误访问

Posted

技术标签:

【中文标题】在将 UITapGestureRecognizer 添加到 UIImageView 时进行错误访问【英文标题】:Taking Bad-Access while Adding UITapGestureRecognizer to UIImageView 【发布时间】:2014-09-02 09:05:10 【问题描述】:

我正在尝试向 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];    

【问题讨论】:

请发布您的 openPhotoDetail: 方法 您的 openPhotoDetail 方法是否与您的 UITapGestureRecognizer init 属于同一类? openPhotoDetail 方法在哪里? 您在错误日志中看到[UISwipeGestureRecognizer openPhotoDetail] 吗?那是你的问题。 @BlackFlam3 我曾经在错误日志中看到过这个,现在没有错误日志,只是访问不正确 【参考方案1】:

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

    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】:

试试这个代码

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);


    

【讨论】:

你在你的 .h 文件中添加了委托@erdemgc UISwipeGestureDelegation 与添加手势无关。 你能改变这一行 [mainView bringSubviewToFront:firstVerticalImageView].like this [mainView addSubview:firstVerticalImageView];试着让我知道错误@erdemgc @Sport 我已删除此行 [mainView bringSubviewToFront:firstVerticalImageView] 但仍然无法访问 ***.com/questions/4425041/… 试试这个【参考方案3】:

你的方法不对,应该是这样-

 -(void)openPhotoDetail:(UITapGestureRecognizer *)sender
 

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

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

   

【讨论】:

方法本身未被识别。 :)

以上是关于在将 UITapGestureRecognizer 添加到 UIImageView 时进行错误访问的主要内容,如果未能解决你的问题,请参考以下文章

UITapGestureRecognizer 不适用于 UIView 动画

使用 UITapGestureRecognizer 无法识别的选择器

UIlabel 中的 UITapGestureRecognizer 错误

UITapGestureRecognizer 的问题

带有点击事件的 UITapGestureRecognizer

UIButton 上的 UITapGestureRecognizer