我有一个相当简单的UITableView,它在栈上推送一个新视图.新视图有一个这样初始化的gestureRecognizer @synthesize swipeGestureLeft; - (void)viewDidLoad{ swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithT
@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
如何确保在取消分配视图后不调用委托方法.
解决:我正在使用新的iOS7navigationController.interactivePopGestureRecognizer并将其委托设置为“self”.
在重新分配后,我没有将它的委托设置为“nil”.正是这个对象保留了(不确定是否正确的单词)委托调用而不是swipeLeftGesture对象.在dealloc中将其委托设置为nil就可以了.