检测导航控制器内容更改但忽略导航控制器本身的隐藏/显示

时间:2015-03-31 18:51:57

标签: ios uiviewcontroller uinavigationcontroller

我是应用程序主导航控制器的UINavigationControllerDelegate。我想知道我的导航控制器显示的视图控制器何时即将更改,以及何时更改。我目前使用这些委托方法来找出:

// Called when the navigation controller shows a new top view controller via a push, pop or setting of the view controller stack.
optional func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool)
optional func navigationController(navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool)

不幸的是,导航控制器包含在带有滑出菜单的视图控制器中。菜单控制器调用主视图控制器上的外观转换方法,以便在显示菜单时收到viewWillDisappear,并在隐藏菜单时收到viewWillAppear / viewDidAppear。导航控制器中包含的视图依赖于这些事件来了解它们何时可见。

viewWillAppear在导航控制器本身触发时,它会在我身上调用navigationController:willShowViewController:。当viewDidAppear在导航控制器上触发时,它会调用navigationController:didShowViewController:

有什么方法可以区分我的导航控制器呈现不同的内容视图控制器和导航控制器本身?

1 个答案:

答案 0 :(得分:0)

我认为您可以通过继承导航控制器并添加您在viewWillAppear中设置的BOOL属性来实现这一目标,

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.willBeAppearing = YES;
}

在委托中,检查此属性,并在didShowViewController:方法中将其设置为NO,如果它当前为YES,

-(void)navigationController:(RDNavController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if (navigationController.willBeAppearing == NO) {
        NSLog(@"The controller is changing");
        // Do what you need to do when a new controller is about to appear
    }
}


-(void)navigationController:(RDNavController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if (navigationController.willBeAppearing == NO) {
        NSLog(@"The controller has changed");
        // Do what you need to do when a new controller has appeared
    }else{
        navigationController.willBeAppearing = NO;
    }
}

请注意,我将委托方法中传入的导航控制器的类更改为我的自定义类。