3

我有一个相当简单的 UITableView 在堆栈上推送一个新视图。新视图有一个像这样初始化的手势识别器

@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 方法中我有

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


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



}

在我的 .h 文件中,我有

@interface MyViewController : UIViewController <UIGestureRecognizerDelegate>

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

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

如何确保在视图释放后不调用委托方法。

4

1 回答 1

7

已解决:我正在使用新的 iOS7 navigationController.interactivePopGestureRecognizer 并将其委托设置为 "self" 。

解除分配后,我未能将其代表设置为“nil”。正是这个对象保留了(不确定是否正确)委托调用,而不是 swipeLeftGesture 对象。在 dealloc 中将其委托设置为 nil 就可以了。

于 2013-09-13T02:55:10.727 回答