我有一个相当简单的 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
如何确保在视图释放后不调用委托方法。