views:

224

answers:

2

Hi!

Without giving you all my code examples, I'll made this quick.

Has this ever happened to one of you to get viewWillAppear called only the first time it shows up?

I have this problem with all my view.

For example: When my app starts, I get to the StartView which is a main menu. (viewWillAppear gets called) then I press on one button that'll show a navigation controller (viewWillAppear gets called). Then I get back to the main menu (it does not get called) and then I press on the same navigation controller again and it does not get called.

It would be awesome if someone could points me somewhere, I've been searching for this since two days now...

Also if you need more code samples, I can give you some.

For further reading:

That's how I call my navigation controller:

PremierSoinsAppDelegate *AppDelegate = (PremierSoinsAppDelegate *)[[UIApplication sharedApplication] delegate];

UIView *newView = [AppDelegate.navigationController view];

[newView setFrame:CGRectMake(320.0f, 0.0f, 320.0f, 480.0f)];
[UIView beginAnimations:@"RootViewController" context:nil];
[UIView setAnimationDuration:0.3];
newView setFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
UIView commitAnimations];

[AppDelegate.window addSubview:newView];
[AppDelegate.window makeKeyAndVisible];

And that's how I show my menu back:

PremierSoinsAppDelegate *AppDelegate = (PremierSoinsAppDelegate *)[[UIApplication sharedApplication] delegate];

UIView *newView = [AppDelegate.startViewController view];

newView setFrame:CGRectMake(-320.0f, 0.0f, 320.0f, 480.0f)];
UIView beginAnimations:@"StartViewController" context:nil];
UIView setAnimationDuration:0.3];
newView setFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
[UIView commitAnimations];

[AppDelegate.window addSubview:newView];
[AppDelegate.window makeKeyAndVisible];

Thanks A LOT.

+1  A: 

viewWill/DidAppear: will only be called when using a UINavigationController or UITabBarController (or really any system-provided-viewControlller managing class) to manipulate views. If you're manually doing this (as you seem to do in your second code snippet, these messages won't get sent.

Ben Gottlieb
I'm using @interface RootViewController : UITableViewController { How should I make this? Please help me :/ Thanks!
Tom
You should read up on UINavigationController; you'll want to use -[UINavigationController pushViewController:animated:] to add your new view.
Ben Gottlieb
+1  A: 

You can implement the UINavigationControllerDelegate in your Nav Controller to propagate the viewWillAppear: messages down. You can implement the message like this:

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if ([viewController respondsToSelector:@selector(viewDidAppear:)]) {
        [viewController viewDidAppear:animated];
    }
}

Note that this is the viewDidAppear and not ViewWillAppear version, but they're basically the same.

However, you should note that the fact that you need to do this may be a sign that something else is wrong in your controller/view code and you might want to reask the question giving us more context to answer it. In particular, I'm assuming that somewhere outside of the code you're giving us, you're pushing and popping view controllers as per usual for a Nav Controller.

corprew
note that if you're not pushing and popping things off of the UINavigationController, you might want to go to the apple developer website and pick up one of their samples about how to use the Nav Controller. Will suggest one if that's helpful.
corprew
@corprew: Thanks for your answer. That's why I'm asking here, I'm sure there's something wrong with my code... But I don't know why. I looked at some samples from the Apple Developer Connection (Recipes and NavBar) but did not find something that I could do in my current code... :/I tried something like the code you gave me before, but I don't want to force something that should work that way :/Thanks
Tom