6

So I have two views. One views loads when I start the app. Then when I press a button on that view it loads a another view.

- (IBAction)triggerGame
{

leveldata = [[NSMutableDictionary alloc] init];

[leveldata setValue:@"hide" forKey:@"shootarrow"];

[leveldata writeToFile:[self levelFilePath] atomically:YES];
[leveldata release];


Papertoss_MaraAppDelegate *mainDelegate = (Papertoss_MaraAppDelegate *)[[UIApplication sharedApplication] delegate];
[mainDelegate playGame];

}

This action triggers a method in the delegate implementation called playGame, which looks like this:

- (void)playGame {

[levelView.view removeFromSuperview];
[self.window addSubview:[gameView view]];
[UIView commitAnimations];
}

This loads the new view just fine. Then I have another button that does the same exact thing, but it brings me back to the first view. And it too, works great. I am able to navigate from one view to the other very easily. But the only problem I have, is when I try load the second view a second time, the viewDidLoad is not called again. I tested this by having an NSLog() in the viewDidLoad method.

For my app to do what I want I need the viewDidLoad to be called again. I'm guessing maybe my view isn't fully unloaded when I switch between them.

Any help is greatly appreciated.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
kernelpanic
  • 530
  • 2
  • 8
  • 26

2 Answers2

9

I think you need the function viewDidAppear. viewDidLoad only gets called once per view unless something causes it to unload, such as a memory warning. viewDidAppear gets called every time that view becomes visible.

Alex Gosselin
  • 2,942
  • 21
  • 37
  • What if I need to unload it, because I wanted it to load up different layers depending on which button I press. How would I do that? – kernelpanic Nov 08 '10 at 00:34
  • I don't think releasing the view is the best solution in this case, it could deallocate all that memory then has to reallocate and initialize a new view later – Alex Gosselin Nov 09 '10 at 22:04
  • Yea I know its not the best solution, but I would like to know how to do it. How exactly do I release it? Or maybe I should be asking where should I release it. – kernelpanic Nov 10 '10 at 04:22
  • I got it all figured out. Thanks to everyone for their help! – kernelpanic Nov 10 '10 at 22:16
4

You want -viewDidAppear: for "every time the view is shown" stuff.

Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135