让viewDidAppearviewWillAppear 在页面加载前始终调用

mac2022-06-30  66

viewDidAppear、viewWillAppear in UINavigationController not be calledWhen 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:

1 @interface RootViewController : UIViewController <UINavigationControllerDelegate> { 2 UINavigationController *navController; 3 } 4 5 Then implement these two methods: 6 - (void)navigationController:(UINavigationController *)navigationController 7 willShowViewController:(UIViewController *)viewController animated:(BOOL)animated 8 { 9 [viewController viewWillAppear:animated]; 10 } 11 12 - (void)navigationController:(UINavigationController *)navigationController 13 didShowViewController:(UIViewController *)viewController animated:(BOOL)animated 14 { 15 [viewController viewDidAppear:animated]; 16 }

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.

转载于:https://www.cnblogs.com/w-zhijun/archive/2012/05/14/2499076.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)