【问题标题】:Capturing back click in navigation controller在导航控制器中捕获返回点击
【发布时间】:2010-01-08 16:15:19
【问题描述】:

在导航控制器应用程序中,假设您已为根控制器分配了标题,推送的视图将在其导航栏的左上角有一个带有该标题的后退按钮。那是自动的。如何根据该后退按钮的点击事件执行代码?

【问题讨论】:

    标签: iphone objective-c cocoa-touch


    【解决方案1】:

    实现 UINavigationControllerDelegate

    @protocol UINavigationControllerDelegate <NSObject>
    
    @optional
    
    // Called when the navigation controller shows a new top view controller via a push, pop or setting of the view controller stack.
    - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
    - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
    
    @end
    

    编辑: 一个简单的两级层次结构的例子,但可以很容易地更新到更多)

    使您的根视图控制器成为UINavigationController 委托(例如在viewDidLoad 中),然后按如下方式实现:

    - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
    {
        if (viewController == self )
        {
            if (lastView == theOtherView)
            {
                // Pop from other view to root view
            }
        }
        else if (viewController == theOtherView)
        {
            // push to other view
        }
        lastView = viewController;
    }
    

    【讨论】:

    • 是的,但是如果根控制器有两条路径 - 一个是从根控制器 tableview 推送 view1,另一个是通过信息图标推送 view2,我如何判断我来自哪个视图什么时候回到根控制器?
    • 已更新。将 viewController 参数作为属性 lastView 存储在您的委托类上并进行比较。使用 UINavigationController 属性 topViewController 和 visibleViewController 会更好,但遗憾的是,在调用两个委托方法之前,这些属性似乎已经更新:(
    • 这不只是捕获返回点击,还捕获代码完成的弹出。
    猜你喜欢
    • 1970-01-01
    • 2014-01-21
    • 1970-01-01
    • 1970-01-01
    • 2015-09-08
    • 2015-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多