UINavigationController在IOS 8中被破坏了吗?

时间:2014-10-16 20:37:01

标签: ios objective-c uiviewcontroller uinavigationcontroller ios8

我的应用程序中有一系列UIViewControllers,这些是使用UINavigationController呈现的。 HOME ViewController调用pushViewController来访问PROFILES ViewController。 PROFILES屏幕正确显示HOME的后退按钮。然后选择PROFILES上的按钮将使用户进入DETAIL ViewController。详细信息屏幕正确显示PROFILES的后退按钮。但是当我按下后退按钮到PROFILES时,我确实回到了PROFILES屏幕,但是该屏幕上的导航栏现在显示标题HOME而没有后退按钮。换句话说,看起来IOS 8已经弹出ViewController一次从DETAIL返回PROFILES,但是以某种方式弹出导航栏项两次!

知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

显然,解决方案大纲可以在这里找到: UINavigationController and UINavigationBarDelegate.ShouldPopItem() with MonoTouch

在我的解决方案中,我只是让我的所有视图控制器都扩展了CustomUINavigationController,它看起来像这样:

#import "CustomUINavigationController.h"
#import "IOSVersion.h"

@interface CustomUINavigationController ()

@end

@implementation CustomUINavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.delegate=self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//
// The following two methods are the key to overriding the buggy behavior in IOS 8
// The first method is from here:
// https://stackoverflow.com/questions/6413595/uinavigationcontroller-and-uinavigationbardelegate-shouldpopitem-with-monotouc
//
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {
    NSLog(@"Inside shouldPopItem");
    if (regularPop) {
        NSLog(@"regularPop is TRUE");
    } else {
        NSLog(@"regularPop is FALSE");
    }
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
        if (regularPop) {
            regularPop = FALSE;
            return YES;
        }

        regularPop = TRUE;
        [self popViewControllerAnimated:YES];
        return NO;
    } else {
        return [super navigationBar:navigationBar shouldPopItem:item];
    }
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
    NSLog(@"Inside popViewControllerAnimated");
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
        NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:self.viewControllers];
        int cnt=(int)[viewControllers count];
        NSLog(@"Inside popViewControllerAnimated, cnt is %d",cnt);
        UIViewController *vc=[viewControllers objectAtIndex:cnt-2];
        if (regularPop) self.desiredVC=vc;
        [self popToViewController:vc animated:animated];
        return vc;
    } else {
        return [super popViewControllerAnimated:animated];
    }

}

- (UIViewController *)manualpopViewControllerAnimated:(BOOL)animated {
    NSLog(@"Inside manualpopViewControllerAnimated");
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
        regularPop=TRUE;
        return [self popViewControllerAnimated:animated];
    } else {
        return [super popViewControllerAnimated:animated];
    }
}

- (void)navigationController:(UINavigationController *)navigationController
   didShowViewController:(UIViewController *)viewController
                animated:(BOOL)animated {
    NSLog(@"Inside didShowViewController");
    if (viewController==self.desiredVC) {
        NSLog(@"Inside didShowViewController, found desiredVC");
        regularPop = FALSE;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
相关问题