我正在尝试使用下面的一组代码禁用视图控制器的后退手势。
在FirstViewController.m
中,我设置了interactivePopGestureRecognizer
的委托
- (void) viewWillLoad {
// Other stuff..
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
然后实现<UIGestureRecognizerDelegate>
方法并返回NO
。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
return NO;
}
在dealloc中,我将委托设置为nil。(我在某处读到过,在iOS 7中,您必须手动将委托设置为零)
- (void)dealloc {
self.navigationController.delegate = nil;
self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}
这在FirstViewController
中有效。但当我将SecondViewController
推到这一点时,手势也不起作用。如何仅在FirstViewController中禁用手势?
同样,当我弹出FirstViewController
转到RootViewController
,然后再次尝试推送FirstViewController
时,我得到对象已释放错误:
[FirstViewController gestureRecognizer:shouldReceiveTouch:]: message sent to deallocated instance 0x14ed0280
除了将委托设置为零之外,我还需要做什么?或者我把它放在了错误的地方?
发布于 2013-10-01 13:38:57
在您的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;
}
发布于 2013-10-16 11:46:40
我最初把这些答案放在接受的答案下面的评论中,但我觉得这需要作为一个答案来说,以获得更多的可见性。
通常情况下,你会发现被接受的答案不起作用。这是因为可以在将视图添加到导航控制器的视图层次结构之前调用viewWillAppear:
,因此self.navigationController
将为nil
。因此,在某些情况下可能不会禁用interactivePopGestureRecognizer。您最好在viewDidAppear:
中调用它。
下面是可以工作的代码(假设视图控制器被正确地添加到导航控制器的视图层次结构中):
Objective-C
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[[self navigationController] interactivePopGestureRecognizer] setEnabled:NO];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[[self navigationController] interactivePopGestureRecognizer] setEnabled:YES];
}
斯威夫特
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
navigationController?.interactivePopGestureRecognizer?.isEnabled = false
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.interactivePopGestureRecognizer?.isEnabled = true
}
发布于 2014-04-25 03:04:36
我发现将手势设置为仅禁用并不总是有效。它确实起作用了,但对我来说,它只是在我使用一次回手势之后才起作用的。第二次它不会触发回击手势。此外,正如John Rogers所说,使用viewDidAppear和viewWillAppear作为navigationController是很重要的,否则将是零。
我的修复方法是委托手势并实现shouldbegin方法以返回NO:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// Disable iOS 7 back gesture
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
// Enable iOS 7 back gesture
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
return NO;
}
https://stackoverflow.com/questions/19108601
复制相似问题