1

我正在嘗試將手勢識別器添加到UIImageView,但是訪問不正確。在將UITapGestureRecognizer添加到UIImageView時出現Bad-Access

[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];  
} 
+0

請發表您的openPhotoDetail:方法 – Rohan 2014-09-02 09:11:37

+0

貴openPhotoDetail方法是在同級別比你UITapGestureRecognizer初始化? – streem 2014-09-02 09:31:31

+0

openPhotoDetail方法在哪裏? – avismara 2014-09-02 11:13:42

回答

-1

你的方法是錯誤的,它應該是這樣的 -

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

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

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

    } 
+0

沒有任何變化,仍然不好訪問 – erdemgc 2014-09-02 10:50:09

+0

該方法本身沒有被識別。 :) – avismara 2014-09-02 11:18:54

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


    } 
+0

沒有任何更改,仍然訪問不良 – erdemgc 2014-09-02 10:47:57

+0

您是否在.h文件中添加了委託@erdemgc – Sport 2014-09-02 11:09:09

+0

UISwipeGestureDelegation與添加手勢無關。 – avismara 2014-09-02 11:12:57

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; 
} 
+0

沒有改變,仍然不好訪問 – erdemgc 2014-09-02 10:48:27

+0

先刪除圖片的出口鏈接,然後再連接它。清除代碼並再次嘗試 – 2014-09-02 10:54:24

+0

它不作爲插座鏈接 – erdemgc 2014-09-02 11:15:49

相關問題