我有一个相当简单的UITableView,可以在堆栈上推送新视图。新视图有一个gestureRecognizer,它像这样启动了
@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
如何确保在取消分配视图后不调用委托方法。
答案 0 :(得分:7)
解决:我正在使用新的iOS7 navigationController.interactivePopGestureRecognizer并将其委托设置为“self”。
取消分配后,我未能将其委托设置为“nil”。正是这个对象保留了(不确定是否正确的单词)委托调用而不是swipeLeftGesture对象。在dealloc中将其委托设置为nil就可以了。