iOS View Controller Lifecycle
View Controller Lifecycle Methods in iOS Application
There are three ways to create UI (Which attached to a viewController class).
1. From the .xib
2. From Code
3. By Storyboard
loadView: This is the method that loads or creates the view for the view controller.
We should never call this method directly. The view controller calls this method when its view property is requested but is currently nil.
If the view controller has an associated nib file, this method loads the view from the nib file. If the view controller does not have an associated nib file, this method creates a plain
UIView object instead.
We can override this method in order to create our views manually.
If we are working with storyboards or nib files, then we do not have to do anything with this method and we can ignore it.
viewDidLoad: This method is called when the view is loaded into memory, whether from a Xib file, storyboard or programmatically created in loadView.
When this method gets called, the view of the view controller has been created and we are sure that all the outlets are in place.
This method is called only once in the lifetime of a view controller, so we use it for things that need to happen only once.
This method call before the bounds are defined and rotation happens. So it is risky to work with the view’s size in this method.
viewwillAppear: This Method is called every time before the view is visible to and before any animation is configured.
We override this method for tasks that we need to repeat every time a view controller comes on screen. This method can be called multiple times for the same instance of a view controller.
Notifies the view controller that its view is about to be added to a view hierarchy.
In this method view has bound but orientation is not set yet.
viewDidAppear: This method is called after the view controller appears on the screen.
We can use it to start animations in the user interface, to start playing a video or a sound, or to start collecting data from the network.
viewWillLayoutSubviews: This method is called to notify the view controller that its view is about to layout its subviews.
This method is called every time the frame changes like for example when rotating or it’s marked as needing layout.
viewDidLayoutSubviews: This method is called to notify the view controller that its view has just laid out its subviews. We make additional changes hereafter the view lays out its subviews.
viewWillDisappear: This method is called before the transition to the next view controller happens and the origin view controller gets removed from the screen. This means, the view is still on view hierarchy but not removed yet.
We add code here to handle timers, hide the keyboard, to cancel network requests, revert any changes to the parent UI. Also, this is an ideal place to save the state.
viewDidDisappear: This method is called after the VC’s view has been removed from the view hierarchy.
We usually override this method to stop tasks that are should not run while a view controller is not on screen. For example, stop listening to notifications, monitoring the device sensors or a network call that is not needed anymore.
deinit(): Before a view controller is removed from memory, it gets deinitialized. We usually override deinit() to clean resources that the view controller has allocated that are not freed by ARC.
Keep in mind that just because a VC is no longer visible, doesn’t mean that it has been deallocated. Container view controllers such as NavigationController can keep their VCs available in memory. Keep in mind that even though a VC is off-screen, if it is still in memory, it still works normally and can receive notifications. Sometimes this is desirable, other times it isn’t, so we need to keep this in mind while developing our app.
didReceiveMemoryWarning(): When the memory starts to fill up, iOS does not automatically move data from memory to its limited hard disk space. It does, however, issue this warning and WE are responsible for clearing some objects out of memory. Be aware that if the memory of our app goes over a certain threshold, iOS will shut down your app. Unfortunately, this will look like a crash to the end users.
viewWillTransition(to: with:): When the interface orientation changes, UIKit calls this method on the window’s root view controller before the size changes are about to be made. The root view controller then notifies its child view controllers, propagating the message throughout the view controller hierarchy.