PopViewController奇怪的行为 [英] PopViewController strange behaviour

查看:350
本文介绍了PopViewController奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于一个奇怪的请求,我试图拒绝,但它不起作用,我不得不覆盖navigationBar的后退按钮。

Due to a weird request which I tried to turn down but it didn't work, I had to override the navigationBar's back Button.

我做了一个自定义UINavigationController子类并入侵了
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item 方法。

I have made a custom UINavigationController subclass and hacked the - (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item method.

这是我的代码:

@interface CustomUINavigationController ()

@end

@implementation CustomUINavigationController


#pragma mark - UINavigationBar delegate methods

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {

    if ([[self.viewControllers lastObject] isKindOfClass:[ViewController1 class]]) {
        ViewController1 *vc1 = (ViewController1 *)[self.viewControllers lastObject];
        [vc1 handleBackAction];
        if (vc1.canPopVC == YES) { 
            [self popViewControllerAnimated:YES];
            return YES;
        } else {
            return NO;
        }
    }

    [self popViewControllerAnimated:YES];
    return YES;
}

@end

一切正常,除非是我以编程方式弹出一个viewController。每次我想在弹出后执行推送时,应用程序都会崩溃。在上转动 NSZombie,发现当以编程方式弹出viewController时,其父viewController被释放。
此时,制作自定义backButton不是一个选项,因为它会丢失原生iOS 7滑动到popViewController功能。

All works fine, except when I pop a viewController programmatically. The app crashed every time when I wanted to perform a push after said pop. Turning NSZombie on, revealed that when popping a viewController programmatically, its parent viewController is deallocated. At this point, making a custom backButton is not a option since it will lose the native iOS 7 swipe to popViewController feature.

崩溃日志:

*** -[ContactsDetailViewController performSelector:withObject:withObject:]: message sent to deallocated instance 0x1806b790


推荐答案

我不是百分百肯定,但我认为你不应该真的在弹出视图该委托方法中的控制器。

I'm not 100% certain but I don't think you should actually be popping the view controller in that delegate method.

应该委托方法通常不会做某事。他们只断言是否应该做某事。

"should" delegate methods don't normally do something. They just assert whether something should or shouldn't be done.

将你的方法改为...

Change your method to this...

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {

    if ([[self.viewControllers lastObject] isKindOfClass:[ViewController1 class]]) {
        ViewController1 *vc1 = (ViewController1 *)[self.viewControllers lastObject];
        [vc1 handleBackAction];
        if (vc1.canPopVC == YES) { 
            return YES;
        } else {
            return NO;
        }
    }

    return YES;
}

看看它是否有效。

我所做的就是删除 popViewController 来电。

All I have done is removed the popViewController calls.

编辑 - 如何添加自定义后退按钮

上的类别中UIBarButtonItem ...

+ (UIBarButtonItem *)customBackButtonWithTarget:(id)target action:(@SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:@"Some image"] forState:UIControlStateNormal];
    [button setTitle:@"Some Title" forState:UIControlStateNormal];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];

    return barButtonItem;
}

现在,只要您想设置自定义后退按钮,只需使用...

Now whenever you want to set a custom back button just use...

UIBarButtonItem *backButton = [UIBarButtonItem customBackButtonWithTarget:self action:@selector(backButtonPressed)];

这篇关于PopViewController奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆