在导航控制器应用程序中,假设您已为根控制器分配了标题,推送的视图将在其导航栏的左上角有一个带有该标题的后退按钮。这是自动的。如何根据该后退按钮的click事件执行代码?
答案 0 :(得分:4)
实施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;
}