开发者

Capturing back click in navigation controller

I开发者_JAVA百科n 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?


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

Edit: example for a simple two level hierarchy, but can easily be updated to more)

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜