2013-05-12 56 views
1

我使用UITapGestureRecognizer来处理ImageviewTap操作。在主ViewController中,它工作得很好。但是,当我在我的APP中使用另一个ViewController,并复制相同的UITapRecognizer代码时,我将一个EXC_BAD_ACCESS代码= 1地址:0x80759d3a错误消息添加到我的imageview识别器中。我错了什么?UITapGestureRecognizer错误,当我添加到图像视图

我的ImageView的:它的工作原理

UIImageView *live; 
live = [[UIImageView alloc]initWithFrame:CGRectMake(92, 230, 136, 100)]; 
live.image = [UIImage imageNamed:@"online.png"]; 
[live addSubview:onlineLabel2]; 
[live setUserInteractionEnabled:YES]; 
[self.view addSubview:live]; 
[super viewDidLoad]; 

和我的手势识别器:

UITapGestureRecognizer *singleTaP3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onlineTap:)]; 
singleTaP3.numberOfTapsRequired = 1; 
singleTaP3.numberOfTouchesRequired = 1;  
[live addGestureRecognizer:singleTaP3]; 

最后一行我得到的崩溃。

+0

首先,您应该调用[super viewDidLoad];首先,然后添加代码。 – danypata 2013-05-12 18:46:55

回答

0

我的问题是在我的主视图控制器。我把这个新的viewcontroller称为错误的,并为它指定了空指针。获取它的财产,它的工作原理。问题不在于gesturetaprecognizer。

0

听起来好像您的实时视图不会保留,所以当您尝试添加手势时,该视图不再存在。尝试在您的界面中将您的实时视图记录为属性,并在您的实现中合成它。

+0

试过这个,但它不工作:(同一个错误,相同的行 – tomeszmh 2013-05-12 19:18:53

+0

你能否给我们你的 - (void)viewDidLoad方法和你的接口? – Franck 2013-05-12 19:25:50

+0

[链接] http://pastebin.com/mg1SeNf4 [链接] http ://pastebin.com/5DTq7Egm – tomeszmh 2013-05-12 19:30:31

0

我不知道如果这导致你崩溃的代码,但是你应该叫

[super viewDidLoad]; 

在代码的开始。 (如果你使用的是ARC,看起来不错)。

[super viewDidLoad]; 
UIImageView *live; 
live = [[UIImageView alloc]initWithFrame:CGRectMake(92, 230, 136, 100)]; 
live.image = [UIImage imageNamed:@"online.png"]; 
[live addSubview:onlineLabel2]; 
[live setUserInteractionEnabled:YES]; 
[self.view addSubview:live]; 
+0

我使用ARC,但它不起作用 – tomeszmh 2013-05-12 19:22:15

+0

如果你使用ARC,你应该声明你的属性是这样的(strong,not retain):@ property(nonatomic,strong)UIImageView * live; – Franck 2013-05-12 19:35:00

+0

我设置为属性@属性(非原子,强)UIImageView *生活; 并添加gesturerecognizer到_liv e,同样的错误 – tomeszmh 2013-05-12 19:48:59

0

我测试了以下,它的工作原理。 接口:

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController 

@property (strong, nonatomic) UIView *v; 

- (void)tapAction; 

@end 

和实现:

#import "ViewController.h" 

@implementation ViewController 

@synthesize v=_v; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    _v = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)]; 
    [_v setBackgroundColor:[UIColor redColor]]; 

    [self.view addSubview:_v]; 

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)]; 
    tap.numberOfTouchesRequired = 1; 
    tap.numberOfTapsRequired = 1; 

    [_v addGestureRecognizer:tap]; 
} 

- (void)tapAction 
{ 
    NSLog(@"tap tap"); 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end