This is for latest iOS Versions. Mainly there are 8 different stages which makes the lifecycle of a UIViewController. I have listed them according to the order.
View Management- A view controller uses the following methods to manage its views-
You should never call this method manually. It is automatically called when its view property is accessed. It loads or creates a view and assigns it to the view property. Override this method in order to create view controller’s default view programmatically. If you use Interface Builder to create view controller’s views, you must not override this method.
It is automatically called when the view controller loaded completely in the memory.But the bounds for the view are not defined yet. Override this method to perform additional initialization on views that were loaded from nib files and other tasks. E.g.-
- Instance variable initialization
- XML parsing
- Network request
- Database access
- Heavy object allocation
Event Management to Views- A view controller uses the following methods to manage events to its views-
This event notifies the view controller whenever the view appears on the screen i.eview is about to be added to the view hierarchy. In this step the view has bounds that are defined but the orientation is not set. Override this method to perform custom tasks associated with displaying the view. E.g.-
- Update navigation bar style
- Update status bar style
- Update screen data
- Orientation handling
This is the first step in the lifecycle where the bounds are finalized. If you are not using constraints or Auto Layout you probably want to update the subviews here.
This event notifies the view controller that the subviews have been setup. It is a good place to make any changes to the subviews after they have been set.
The viewDidAppear event fires after the view is presented on the screen i. e.view controller’s view was added to the view hierarchy. Override this method to perform additional tasks associated with displaying the view. E.g.-
- Start UI animation
- Display loader view
It is called when the view controller’s view is about to be removed from the view hierarchy. Override this method to perform following tasks-
- Commit editing changes
- Hide keyboard
- Revert changes that were made in viewWillAppear method
It is called when the view controller’s view was removed from the view hierarchy. Override this method to perform additional tasks associated with dismissing or hiding the view. E.g.-
- Remove cache data
- Stop services related to view like audio, bonjour service.
Memory Management- A view controller use this method to manage memory to its views-
It is called automatically when the system determines that the amount of available memory is low. Override this method to release any additional memory that is not essential. E.g.-
- Remove cache
- Release instance variables
- Release system resources like user’s contacts database
Hope you find this blog useful. Please feel free to contact with me in case you have any query, suggestions. You can comment, like and follow posts.
You can request any topic related to Swift and iOS development.