iphone - 捕获导航 Controller 中的后退单击

标签 iphone objective-c cocoa-touch

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

最佳答案

实现 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

编辑: 简单的两级层次结构的示例,但可以轻松更新为更多)

使您的 Root View Controller 成为 UINavigationController 委托(delegate)(例如在 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;
}

关于iphone - 捕获导航 Controller 中的后退单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2028925/

相关文章:

objective-c - 向 RACSubscriber 发送错误时需要返回吗?

iphone - iOS 5 和 iOS 6 - UIWebView 的行为差异

iphone - 由 Adob​​e Flash Professional CS5(非 CS5.5)开发的应用程序在 iPhone 4 上的 App Store 中可见吗?

Objective-C "if not equal or"语句不起作用

ios - 在数组中查找日期最接近当前时间的对象

iphone - 为 UITableView 设置三边边框 - IOS

ios - View 上的 UISwipeGestureRecognizer - 如果触摸位于 UITableViewCell 内部,如何将其传递到单元格?

iphone - 单击按钮时 NSMutable 数组到 UITableview

iphone - 从 NavigationController 推送时如何全屏绘制 UIViewController?

iphone - 使用按钮更改页面控件