Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When I switch back and forth between views, my viewDidLoad is not being called a second time?

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.

like image 315
kernelpanic Avatar asked Nov 08 '10 00:11

kernelpanic


People also ask

How often is viewDidLoad called?

viewDidLoad() is one of the initialization methods that is called on the initial view controller. viewDidLoad() is called before anything is shown to the user - and it is called only once.

Is viewDidLoad called once?

viewDidLoad() Called after init(coder:) when the view is loaded into memory, this method is also called only once during the life of the view controller object. It's a great place to do any view initialization or setup you didn't do in the Storyboard.

What is called before viewDidLoad?

Yes, viewDidLoad method is called before viewDidAppear:. viewDidLoad and viewDidLoad: mean actually different things. You specify : if it has an argument, but viewDidLoad does not, just as a convention. Loaded in memory means ready to use/display. Follow this answer to receive notifications.

Which is called first viewDidLoad or viewWillAppear?

viewWillAppear is called just before the view is displayed. This happens always after viewDidLoad and is called every time the view is displayed.


2 Answers

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.

like image 70
Alex Gosselin Avatar answered Oct 21 '22 08:10

Alex Gosselin


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

like image 43
Joshua Nozzi Avatar answered Oct 21 '22 09:10

Joshua Nozzi