在导航 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/