【发布时间】:2010-01-08 16:15:19
【问题描述】:
在导航控制器应用程序中,假设您已为根控制器分配了标题,推送的视图将在其导航栏的左上角有一个带有该标题的后退按钮。那是自动的。如何根据该后退按钮的点击事件执行代码?
【问题讨论】:
标签: iphone objective-c cocoa-touch
在导航控制器应用程序中,假设您已为根控制器分配了标题,推送的视图将在其导航栏的左上角有一个带有该标题的后退按钮。那是自动的。如何根据该后退按钮的点击事件执行代码?
【问题讨论】:
标签: iphone objective-c cocoa-touch
实现 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;
}
【讨论】: