问题描述
我正在尝试使用以下代码集为视图控制器禁用后退手势。
I am trying to disable the back gesture for my view controller using the following set of code.
在 FirstViewController.m
,我正在设置的代表:interactivePopGestureRecognizer
- (void) viewWillLoad {
// Other stuff..
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
然后实施< UIGestureRecognizerDelegate>
方法并返回否
。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
return NO;
}
在dealloc中我将委托设置为nil。 (我在某处看过,在iOS 7中你必须手动将代表设置为nil)
And in dealloc I'm setting the delegate to nil. (I have read somewhere that in iOS 7, you have to manually set the delegates to nil)
- (void)dealloc {
self.navigationController.delegate = nil;
self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}
这适用于 FirstViewController
。但是,当我将 SecondViewController
推送到此时,手势也无效。如何才能在FirstViewController中禁用手势?
This works in the FirstViewController
. But when I push SecondViewController
to this, the gesture does not work on that either. How can I disable the gesture in FirstViewController only?
当我弹出 FirstViewController
转到 RootViewController
然后再次尝试推送 FirstViewController
,我得到对象解除分配错误:
Also when I pop FirstViewController
to go to RootViewController
and then try to push FirstViewController
again, I get the object deallocated error :
[FirstViewController gestureRecognizer:shouldReceiveTouch:]: message sent to deallocated instance 0x14ed0280
为什么除了将委托设置为nil之外我还需要做什么?或者我将它设置在错误的位置?
Why else do I need to do other than setting the delegates to nil? Or am I setting it in the wrong place?
推荐答案
在FirstViewController中尝试以下未经测试的代码:
Try the below untested code in your FirstViewController :
-(void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
-(void) viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}
这篇关于如何在iOS 7中仅针对一个视图禁用后退手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!