捕获导航控制器中的点击

捕获导航控制器中的点击

本文介绍了捕获导航控制器中的点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In a navigation controller app, assuming you've assigned a title to the root controller, the pushed view will have a back button with that title in the top left of its navigation bar. That is automatic. How can I execute code based on the click event of that back button?

推荐答案

实现UINavigationControllerDelegate

Implement 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 ),然后实现如下:

Make your root view controller the UINavigationController delegate (e.g. in viewDidLoad) and then implement as follows:

- (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;
}

这篇关于捕获导航控制器中的点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 00:16