Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does viewDidLoad get called more than once in UITabBarController?

I'm using SWRevealViewController in my app (to get the slide out side panel) however whenever the user navigates to another viewController like 'settings' and comes back, everything gets reset. I understand this is normal behaviour for storyboards since a new VC is instantiated and viewDidLoad is called each time. I tried to get around this by storing the VC in an array in the AppDelegate and then going back to the original viewController, this prevented viewDidLoad being called when the original VC is initially re-presented but I still found it get's called randomly when moving between veiwcontroller's, resetting all my properties etc.. On researching, the Apple documentation does say not to assume viewDidLoad will only be called once.

Is this behaviour apparent in UITabBarController when switching tabs as I'm thinking of ditching the SWRevealViewController and using that instead if it's going to be less headache.

Should I be handling this differently, ie. storing the 'state' in NSUserDefaults and restoring on viewDidLoad?

Thanks in advance.

like image 204
Ash R Avatar asked Oct 10 '13 09:10

Ash R


2 Answers

viewDidLoad is called exactly once, when the UIViewController is first loaded into memory. This is where you want to instantiate any instance variables and build any UIViews that live for the entire lifecycle of this UIViewController.

In UITabBarController also the viewdidLoad for UIViewController is called once, when you are switching tabs.

like image 108
Toseef Khilji Avatar answered Nov 15 '22 02:11

Toseef Khilji


viewDidLoad() method is called only once. Its an integral part of the cycle.

It is called then the respective UIViewController class is loaded into memory.

And yes, if you want to initialise any properties or access and modify the NSUserDefaults, it can and should be done in the viewDidLoad method.

As for your app, whenever the user will switch between different UIViewControllers, the viewDidLoad method will be called for every destination UIViewController.

Also, as correctly pointed out, it'll also be called in the case of a memory warning.

like image 2
Sam Fischer Avatar answered Nov 15 '22 02:11

Sam Fischer