viewWillAppear不执行的解决办法
UITableViewController或UIViewController中使()
后viewWillAppear:(BOOL)animated不执行
解决方法如下:
先在 *.h 头文件里加上<UINavigationControllerDelegate>
接着在*.m 文件里加上相关代码
self.navigationController.delegate=self;
再使用此时navigationController的代理函数,就OK了。
-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[viewController viewWillAppear:animated];
}
-(void)viewWillAppear:(BOOL)animated
{
NSLog(@"现在可以使用了");
}
代码实例:
MyOrders.h
@interface MyOrders : UIViewControllerlegate>{
}
@property(nonatomic,retain)IBOutlet UITableView *table;
MyOrders.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.delegate=self;
}
-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[viewController viewWillAppear:animated];
}
-(void)viewWillAppear:(BOOL)animated
{
NSLog(@"现在可以使用了");
}
另外
http://davidebenini.it/2009/01/03/viewwillappear-not-being-called-inside-a-uinavigationcontroller/
后viewWillAppear:(BOOL)animated不执行
解决方法如下:
先在 *.h 头文件里加上<UINavigationControllerDelegate>
接着在*.m 文件里加上相关代码
self.navigationController.delegate=self;
再使用此时navigationController的代理函数,就OK了。
-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[viewController viewWillAppear:animated];
}
-(void)viewWillAppear:(BOOL)animated
{
NSLog(@"现在可以使用了");
}
代码实例:
MyOrders.h
@interface MyOrders : UIViewControllerlegate>{
}
@property(nonatomic,retain)IBOutlet UITableView *table;
MyOrders.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.delegate=self;
}
-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[viewController viewWillAppear:animated];
}
-(void)viewWillAppear:(BOOL)animated
{
NSLog(@"现在可以使用了");
}
另外
http://davidebenini.it/2009/01/03/viewwillappear-not-being-called-inside-a-uinavigationcontroller/
I just stumbled on a small problem that gave me a great headache. I changed the view hierarchy to implement flipping preferences on the back of my App’s main view. Accordingly, I had to move some stuff out of the nibs and into the code. Iin other words, some heavy refactoring, and without testing (I know, bad bad me).
So basically I now have a root
UIViewController which has a UINavigationController as a subview; this, in turn, loads a number of UIViewControllers to navigate my items hierarchy.
Today, while working on cosmestic/usability improvements, I realised that something was definitely not working. When moving back and forth in my items, data were not refreshed. This pointed to one culprit, viewWillAppear not being called in the UIViewControllers.
After some googling I found out that if you add a
UINavigationController as a subview of a UIViewController subclass, you must explicitly call its viewWillAppear method from its container; otherwise, they won’t be called, and when moving back and forth in the navigation tree, your UIViewControllers’ viewWillAppear: methods won’t be called.
It’s quite simple; assuming that
projectNavigationController is a navigation controller added as a a subview of this UIViewController subclass, just make sure you add this simple call:
Of course the same applies to your other viewWill/Did methods
It’s quite simple, but wasn’t obvious for me at first.
评论
发表评论