首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在iOS 7中仅为一个视图禁用后退手势

如何在iOS 7中仅为一个视图禁用后退手势
EN

Stack Overflow用户
提问于 2013-10-01 13:27:51
回答 5查看 8K关注 0票数 5

我正在尝试使用下面的一组代码禁用视图控制器的后退手势。

FirstViewController.m中,我设置了interactivePopGestureRecognizer的委托

代码语言:javascript
复制
- (void) viewWillLoad {

    // Other stuff..
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
}

然后实现<UIGestureRecognizerDelegate>方法并返回NO

代码语言:javascript
复制
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

     return NO;
}

在dealloc中,我将委托设置为nil。(我在某处读到过,在iOS 7中,您必须手动将委托设置为零)

代码语言:javascript
复制
- (void)dealloc {

    self.navigationController.delegate = nil;
    self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}

这在FirstViewController中有效。但当我将SecondViewController推到这一点时,手势也不起作用。如何仅在FirstViewController中禁用手势?

同样,当我弹出FirstViewController转到RootViewController,然后再次尝试推送FirstViewController时,我得到对象已释放错误:

代码语言:javascript
复制
[FirstViewController gestureRecognizer:shouldReceiveTouch:]: message sent to deallocated instance 0x14ed0280

除了将委托设置为零之外,我还需要做什么?或者我把它放在了错误的地方?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2013-10-01 13:38:57

在您的FirstViewController中尝试以下未经测试的代码:

代码语言:javascript
复制
-(void) viewWillAppear:(BOOL)animated 
{
    [super viewWillAppear:animated];
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

-(void) viewWillDisappear:(BOOL)animated 
{
    [super viewWillDisappear:animated];
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}
票数 24
EN

Stack Overflow用户

发布于 2013-10-16 11:46:40

我最初把这些答案放在接受的答案下面的评论中,但我觉得这需要作为一个答案来说,以获得更多的可见性。

通常情况下,你会发现被接受的答案不起作用。这是因为可以在将视图添加到导航控制器的视图层次结构之前调用viewWillAppear:,因此self.navigationController将为nil。因此,在某些情况下可能不会禁用interactivePopGestureRecognizer。您最好在viewDidAppear:中调用它。

下面是可以工作的代码(假设视图控制器被正确地添加到导航控制器的视图层次结构中):

Objective-C

代码语言:javascript
复制
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [[[self navigationController] interactivePopGestureRecognizer] setEnabled:NO];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[[self navigationController] interactivePopGestureRecognizer] setEnabled:YES];
}

斯威夫特

代码语言:javascript
复制
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    navigationController?.interactivePopGestureRecognizer?.isEnabled = false
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    navigationController?.interactivePopGestureRecognizer?.isEnabled = true
}
票数 17
EN

Stack Overflow用户

发布于 2014-04-25 03:04:36

我发现将手势设置为仅禁用并不总是有效。它确实起作用了,但对我来说,它只是在我使用一次回手势之后才起作用的。第二次它不会触发回击手势。此外,正如John Rogers所说,使用viewDidAppear和viewWillAppear作为navigationController是很重要的,否则将是零。

我的修复方法是委托手势并实现shouldbegin方法以返回NO:

代码语言:javascript
复制
- (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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19108601

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档