gestureRecognizer shouldReceiveTouch 在解除分配的视图中持续导致崩溃 [英] gestureRecognizer shouldReceiveTouch persisting in deallocated view causing crash

查看:12
本文介绍了gestureRecognizer shouldReceiveTouch 在解除分配的视图中持续导致崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当简单的 UITableView,它在堆栈上推送一个新视图.新视图有一个这样初始化的gestureRecognizer

I have a fairly simple UITableView that pushes a new view on the stack. The new view has a gestureRecognizer that is initizalied like this

@synthesize swipeGestureLeft;


    - (void)viewDidLoad
{
        swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(toggleViewLeft)];
        swipeGestureLeft.numberOfTouchesRequired = 1;
        swipeGestureLeft.delegate=self;
        swipeGestureLeft.direction = (UISwipeGestureRecognizerDirectionLeft);
        [self.view addGestureRecognizer:swipeGestureLeft];
}

我也调用了委托方法

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

    if (viewShown==1) {
        return NO;
    }
    return YES;
}

在我有的 dealloc 方法中

and in the dealloc method I have

- (void)dealloc {
    NSLog(@"I AM IN DEALLOC");


    swipeGestureLeft.delegate=nil;
    [self.view removeGestureRecognizer:swipeGestureLeft];
    swipeGestureLeft=nil;



}

在我的 .h 文件中

@interface MyViewController : UIViewController <UIGestureRecognizerDelegate>

现在,当我回击返回我的表视图时,视图被释放(我可以看到因为 NSLog 触发)现在当我尝试向下滑动我的表视图时,应用程序崩溃了:

now when I hit back to go back to my table view, the view gets deallocated (which I can see becasue the NSLog fires) now when I try and swipe down on my table view the app crashes with:

[MyViewController gestureRecognizer:shouldReceiveTouch:]: message sent to deallocated instance 

如何确保在视图解除分配后不调用委托方法.

How do I ensure the delegate method is not called after the view has deallocated.

推荐答案

SOLVED: 我正在使用新的 iOS7navigationController.interactivePopGestureRecognizer 并将其委托设置为self".

SOLVED: I am using the new iOS7 navigationController.interactivePopGestureRecognizer and had set its delegate to "self" .

在解除分配时,我未能将其委托设置为nil".正是这个对象保留了(不确定是否正确)委托调用而不是 swipeLeftGesture 对象.在 dealloc 中将其委托设置为 nil 就成功了.

Upon deallocation I failed to set it's delegate to "nil". It was this object that was retaining (not sure if right word) the delegate call and not the swipeLeftGesture object. Setting its delegate to nil in the dealloc did the trick.

这篇关于gestureRecognizer shouldReceiveTouch 在解除分配的视图中持续导致崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆