5

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

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 中,您必须手动将代表设置为 nil)

- (void)dealloc {

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

这适用于FirstViewController. 但是当我推SecondViewController到这个时,这个手势也不起作用。如何仅在 FirstViewController 中禁用手势?

此外,当我弹出FirstViewControllerRootViewController然后尝试FirstViewController再次推送时,我得到对象释放错误:

[FirstViewController gestureRecognizer:shouldReceiveTouch:]: message sent to deallocated instance 0x14ed0280

除了将代表设置为零之外,我还需要做什么?还是我把它放在了错误的地方?

4

6 回答 6

24

在您的 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-01T05:38:56.583 回答
17

我最初将这些答案放在已接受答案下方的评论中,但我觉得需要将其作为答案来获得更多可见度。

通常,您会发现接受的答案不起作用。这是因为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
}
于 2013-10-16T03:46:39.597 回答
3

我尝试了以上所有方法,但它们对我不起作用。所以我尝试了这个,它在 IOS7 和 IOS8 上都适用。

只需确保您的视图控制器实现此协议,即UIGestureRecognizerDelegate 并编写下面给出的代码。

-(void)viewWillAppear : (BOOL) 动画 {

[super viewWillAppear : animated];

if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {

    self.navigationController.interactivePopGestureRecognizer.enabled =

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

}

-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {

if ([gestureRecognizer isEqual:self.navigationController.interactivePopGestureRecognizer]) {

    return NO;

} else {

    return YES;

}

}

于 2014-12-19T11:13:29.937 回答
0

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

对我来说,修复是委托手势并实现 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;
}
于 2014-04-24T19:04:35.910 回答
0

这在 xCode 7 中对我有用:

override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        self.navigationController!.interactivePopGestureRecognizer!.enabled = false

}
override func viewWillDisappear(animated: Bool) {
    super.viewDidDisappear(animated)
    self.navigationController!.interactivePopGestureRecognizer!.enabled = true
}
于 2015-09-14T18:29:37.353 回答
-1

只有一个视图,我不知道方法......但我使用下一个代码完全禁用滑动手势:

在你的 AppDelegate.m

if ([[UIDevice currentDevice].systemVersion floatValue] >= 7){
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
于 2013-10-24T07:26:42.210 回答