开发者

How to trigger viewWillAppear when going back in the navigation controller hierarchy?

I've been trying various methods to trigger some code when returning to the first level of a UINavigationController, but none of the below work...

// HomeViewController.m
-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    NSLog(@"trigger 1");
}

- (void)viewWillAppear:(BOOL)animated {
    NSLog(@"trigger 2");
}

-(void) viewDidAppear:(BOOL)animated {
    NSLog(@"trigger 3");
}

I'm starting to think I've set up my app wrong.. is it something about having UIViewControllers within UIViewControllers?

This is the nib for the app.. it's a UITabBarController with a navigation controller for each tab, and within that the first page of开发者_开发问答 the UINavigationController. Is this setup the reason why the methods are not triggering?

How to trigger viewWillAppear when going back in the navigation controller hierarchy?

Not shown in this is a UIView at the bottom which is linked to the File Owner's view. And to make it more complicated, MainViewController is actually the flip side of a Utility-style app.


Just to say I solved this another way. I needed to call something like viewWillAppear because I needed to update my first view table when something had changed in my second view.

The way I achieved it was, in my 'tableView: commitEditingStyle' method (called after I delete something), I put this at the end..

[[[[self.navigationController.tabBarController.viewControllers objectAtIndex:0] viewControllers] objectAtIndex:0] reloadTheTable];

reloadTheTable is a method in my HomeViewController which does stuff like update arrays and [tableView reloadData] etc;

That's a lot of brackets. I'm sure my nib can be set up to make that simpler. I think Kasparov has said that he thinks four moves ahead... and four brackets is my limit.

I worked it out by doing things like this:

NSString *name = NSStringFromClass ([[self parentViewController] class]);
NSLog(@"%@", name); // outputs UINavigationController

and slowly adding to it until I found what I was looking for:

name = NSStringFromClass ([[[[self.navigationController.tabBarController.viewControllers objectAtIndex:0] viewControllers] objectAtIndex:0] class]); //UINavigationController
NSLog(@"%@", name); // outputs HomeViewController

There is something definitely odd in the way viewWillAppear gets cancelled when you puts views within views.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜