让viewDidAppear/viewWillAppear 在页面加载前始终调用
viewDidAppear/viewWillAppear in UINavigationController not be called
When you push or pop a view controller on/off a navigaction controller's stack, the usual viewWillAppear / viewDidappear methods aren't called. If you want to ensure they're always called, just add the UINavigationControllerDelegate protocol to your root view controller:
@interface RootViewController : UIViewController <UINavigationControllerDelegate> {
UINavigationController *navController;
}
Then implement these two methods:
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[viewController viewWillAppear:animated];
}
- (void)navigationController:(UINavigationController *)navigationController
didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[viewController viewDidAppear:animated];
}
Be sure to set the root view controller as the delegate for the nav controller. Now viewWillAppear / viewDidAappear will be called whenever a controller is pushed/popped from the stack.
If you want to call the viewWillDisappear/viewDidDisappear methods, your view controller still has to do that manually before popping itself off the nav stack.

本文介绍了一种确保在iOS应用中,无论何时推入或弹出导航控制器堆栈上的视图控制器时,都能始终调用viewWillAppear/viewDidAppear方法的方法。通过实现UINavigationControllerDelegate协议并覆盖navigationController:willShowViewController:animated:与navigationController:didShowViewController:animated:方法,可以强制这些生命周期方法在控制器被推入或弹出时执行。
6311

被折叠的 条评论
为什么被折叠?



