【问题标题】:gestureRecognizer shouldReceiveTouch persisting in deallocated view causing crash手势识别器 shouldReceiveTouch 坚持在已释放的视图中导致崩溃
【发布时间】:2013-09-13 01:01:32
【问题描述】:

我有一个相当简单的 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 

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

【问题讨论】:

  • 你如何声明 swipeGestureLeft?
  • @property (strong,nonatomic) UISwipeGestureRecognizer *swipeGestureLeft;
  • 嗯。不知道这里发生了什么。我将您的代码放入主从模板中,但无法复制错误。
  • @dmur 感谢您的帮助。我用下面的答案解决了它。

标签: ios uitableview uigesturerecognizer dealloc


【解决方案1】:

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

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

【讨论】:

  • 我尝试在dealloc 中将代理设置为 nil,但这似乎为时已晚。在viewWillDisappear 中将代表设置为零或覆盖UINavigationController 并正确设置代表已成功。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-20
  • 2011-03-17
  • 1970-01-01
  • 2011-09-08
  • 2015-06-26
  • 2011-08-07
相关资源
最近更新 更多