你可曾遇到過viewWillAppear沒有被調(diào)用到的情況
產(chǎn)生原因是用了UINavigationController. 將UINavigationController的view作為subview添加到了其他viewController的view中钮孵。或者把UINavigationController添加到UITabbarController中了捧韵。此時,NavigationController的stack里面的viewController就收不到-(void)viewWillAppear:(BOOL)animated势篡;等4個方法的調(diào)用镀虐。
原因呢
Apple Docs state:
Warning: If the view belonging to a view controller is added to a view hierarchy directly, the view controller will not receive this message. If you insert or add a view to the view hierarchy, and it has a view controller, you should send the associated view controller this message directly. Failing to send the view controller this message will prevent any associated animation from being displayed.
不過后面的到4.0的文檔就沒有發(fā)現(xiàn)這樣的文字描述了突照,但是還是沒能夠調(diào)用的到這樣
只是添加的更復(fù)雜的文檔,頭暈暈看不下去了直颅。
解決方法兩種:
-
在導(dǎo)航控制器上層controller的viewWillAppear中顯式調(diào)用viewWillAppear方法博个。
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[subNavCntlr viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[subNavCntlr viewWillDisappear:animated];
}
2. 把導(dǎo)航控制器上層controller設(shè)為UINavigationController的delegate
nav.delegate = self;
@interface RootViewController : UIViewController
<UINavigationControllerDelegate> { UINavigationController
*navController;}
- (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];
}