5

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.

Ash R
  • 195
  • 4
  • 12
  • the behavior of NavigationController and TabBar are different. In Navigation controller if you have 1(root),2,3 viewControllers and are at viewController1, then you only have 1. if you go to 2, then you have 1,2 if you go back to 1, you have 1 (2 is gone ie set to nil), if you go back to 2 then 2 is created from scratch. If you go 1,2,3 then you have all 3. If this was in tabBar, at all moments you have 1,2,3 regardless of being on the screen or not – mfaani Sep 17 '16 at 01:07

2 Answers2

8

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.

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121
  • **1)** What could make it get loaded again? What if we had low memory and the app itself erased the viewcontroller... would `viewDidLoad` be called the next time it's opened? **2)** Also doesn't it matter **how** the viewController was created? ie if you're naively creating the viewController each time you access it? <-- I only read about this, but don't know how it can be done. I would appreciate if you can elaborate on this. – mfaani Sep 13 '16 at 12:19
  • hello, do you which method should I use to fire every time I click on the tab ?? – Nadeem Khoury Nov 02 '16 at 09:00
  • @NadeemKhoury: You can use `UITabBarControllerDelegate` methods. – Toseef Khilji Nov 02 '16 at 09:35
2

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.

Sam Fischer
  • 1,442
  • 19
  • 35
  • 1
    A memory warning can cause it to be called again. – BooRanger Oct 10 '13 at 10:09
  • But that is unrelated. And it won't matter. True that I should've acknowledged that in my answer however as for his app, I don't think it would be much of an issue. Don't you think ? Because he talking about recreatable variables. – Sam Fischer Oct 10 '13 at 10:30
  • I'd be suprised if my app is getting memory warnings, it's only very basic but I'll test for this. I'll try and update my original post with my code. – Ash R Oct 10 '13 at 11:21
  • 1
    The documentation for [- viewDidUnload](https://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UIViewController_Class/index.html#//apple_ref/occ/instm/UIViewController/viewDidUnload) says that in iOS 6 and later, "views are no longer purged under low-memory conditions." But, [Apple may still call `viewDidLoad` more than once](http://stackoverflow.com/questions/13152304/viewdidload-in-ios-6-called-once). – ma11hew28 Nov 12 '14 at 16:31