【问题标题】:iOS - Identify what UIViewController was previously shown in a UINavigationControlleriOS - 识别 UIViewController 之前在 UINavigationController 中显示的内容
【发布时间】:2012-02-10 17:16:16
【问题描述】:

我需要确定 UIViewController 之前在 UINavigationController 中显示的内容。我的导航深度为 3 级,在第 2 级中,我需要确定我是从第 1 级推送到这里还是从第 3 级弹出到这里。我怎样才能轻松做到这一点?

【问题讨论】:

    标签: objective-c ios cocoa-touch uinavigationcontroller


    【解决方案1】:

    实现 UINavigationControllerDelegate 方法:

     - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
    

    并查询它以找出当前显示的视图控制器:

     navigationController.topViewController
    

    这就是你的来历。

    【讨论】:

    • 我可能很愚蠢,但我不明白。我在我的 2 级视图控制器中实现了 UINavigationControllerDelegate 方法。当我NSLog(@"%@",navigationController.topViewController); 时,我只得到当前正在显示的UIViewController(2 级)(不是我导航的 3 级视图控制器或 1 级视图控制器)。
    • @PeterWarbo 你不傻,我是。对于那个很抱歉。没有简单的方法,但这里有一个应该有效的方法。无论哪个类实例化您的导航控制器,都应使其成为 navigationController.topViewController 属性的 KVO(键值观察器)。每当此属性更改时,它将调用 observeValueForKeyPath: 并在上下文参数中提供更改字典。从中您可以确定旧值是什么。
    【解决方案2】:

    您可以使用 viewControllers 属性查看整个 UINavigationController 堆栈。

    int count = [navigationController.viewControllers count];
    
    topController = [navigationController.viewControllers objectAtIndex:count - 1];
    previousController = [navigationController.viewControllers objectAtIndex:count - 2];
    //...
    //...
    rootController = [navigationController.viewControllers objectAtIndex: count - count];
    

    【讨论】:

      【解决方案3】:

      iOS 5.0 添加了[UIViewController isMovingToParentViewController]。在viewWillAppearviewDidAppear 期间,如果您来自编号较小的视图控制器,则返回YES,否则返回NO。不幸的是,这个名字令人困惑——你会认为[UIViewController isMovingFromParentViewController] 是正确的调用方法。

      但是,我代表所有第二代设备卡在 iOS 4.2.1 的小气鬼,请不要使用 iOS 5 的功能,除非你真的必须这样做。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-08-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多