UITapGestureRecognizer Singletap崩溃

时间:2014-07-28 20:35:49

标签: ios crash uitapgesturerecognizer

这就是我的ViewController的外观 -

#import "DialogViewController.h"



 @interface DialogViewController ()

 @end

 @implementation DialogViewController

 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
 {
     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
     if (self) {
         // Custom initialization
     }
     return self;
 }

 - (void)viewDidLoad
 {
     [super viewDidLoad];

      UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc]
                                                           initWithTarget:self
                                                           action:@selector(handleSingleTap:)];
     [singleTapGestureRecognizer setNumberOfTapsRequired:1];
     [self.view addGestureRecognizer:singleTapGestureRecognizer];}

 - (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
     NSLog (@"Tap");
 }

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

 @end

这是实例化的,并使用 -

从另一个视图中显示
 DialogViewController *dialogViewController = [[DialogViewController alloc]initWithNibName:@"DialogViewController" bundle:nil];
    [self.parentViewController.view addSubview: dialogViewController.view];

如果我点击一下,我的应用就会崩溃EXC_BAD_ACCESS KERN_INVALID_ADDRESS。我的观点没有任何花哨的东西,它只有一个正确显示的图像。我是iOS开发的新手,我确信我在这里做的事情很傻。任何指针都赞赏。

2 个答案:

答案 0 :(得分:0)

当内存被释放并且代码试图访问无效内存时,

EXC_BAD_ACCESS KERN_INVALID_ADDRESS来了。更多代码将帮助您分配视图控制器。但我认为你应该检查你的视图控制器它是如何解除分配的。

保持对视图Controller的强引用作为视图控制器从sowhere解除分配,当你tap找不到指针时。

答案 1 :(得分:-2)

尝试self.view.window

[self.view.window addGestureRecognizer:singleTapGestureRecognizer];