I have the following scenario:
In vcA (when user taps a button in the UI):
back in view of vcA -
repeat the process (to simulate user action of tapping the same button in UI):
So, I am majorly confused.
Thanks in advance.... Sam.
It is called when ever the view appears on the screen. Yes even when the view controller is initialized or when you navigate to the view in a stack.
The Simple Answer. The technical reason for when viewWillAppear gets called is simple. Notifies the view controller that its view is about to be added to a view hierarchy. It can't be any view hierarchy — it has to be the one with a UIWindow at the root (not necessarily the visible window).
Notifies the view controller that its view is about to be removed from a view hierarchy. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.0+ tvOS 9.0+
Swift. Use a NotificationCenter observer rather than viewWillAppear . To find out when an app comes back from the background, use a NotificationCenter observer rather than viewWillAppear .
As you might guess from the name, -viewWillDisappear is called whenever the view controller's view is about to be hidden, removed, etc. Full description on the UIViewController reference page.
-viewDidLoad and -viewWillDisappear are not a matched set. To conserve resources, and because some view controllers may never end up displaying their views, view controllers only load their views the first time they're actually needed. -viewDidLoad is called after that happens.
-viewWillAppear and -viewDidAppear are called just before and after the view is actually displayed. Likewise, -viewWillDisappear and -viewDidDisappear are called before and after the view is no longer visible.
Finally, -viewDidUnload is the counterpart to -viewDidLoad and is called if the view is discarded. That can happen when the system needs to free up some memory, but it may not happen at all.
In iOS 6 viewWillUnload
and viewDidUnload
are Deprecated
So, to address your second question directly, vcB's -viewDidLoad wasn't called a second time because by that time vcB had already loaded its view and didn't need to do it again.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With