UIViewController: Life Cycle when responding to View Events in iOS
While studying Objective-C and iOS development in general, you are going to encounter a class named UIViewController. This class is a very important part of an app development cycle. According to the Apple documentation definition: The UIViewController class provides the infrastructure for managing the views of your iOS apps. A view controller manages a set of views that make up a portion of your app’s user interface. ~ Apple
In simple terms, a view controller is just that: a controller of a view. It manages the presentation of your app’s content on screen by controlling the objects associated with a view. Some responsibilities include the updating of the contents of the views, usually in response to changes to data, responding to user interactions with views, resizing views, managing the layout of the overall interface and related behaviors. For example, when a view changes, a view controller automatically calls its own methods so that subclasses can respond to the change. All these changes usually happen in the following stages according to the Apple’s Documentation:
In this image, you can infer that the Will methods are called before an event is set to appear or disappear. In the other hand, the Did methods are called after the event occurred. Also, note that for every Will method calling, there is a counter action in the form of a Did method showing a full control cycle of a view by a view controller.
However, it is good to note that before this view controller cycle can begin, the viewDidLoad method has to be called first after the controller’s view is loaded into the memory. This is the first method to be verified and it is called only once, as soon as the view in question is called upon or loaded. When it is called by the View Controller automatically, this view controller populates the view with code. It’s a great place to do any view initialization not done in Storyboard. Everything that should be loaded when a view first appears has to be implemented in the viewDidLoad method.
The other methods included in the view controller’s lifecycle are:
- viewWillAppear: This method is called before the view of the view controller is about to be added to a view hierarchy and before any animations are configured for the view. In other words, it is used to prepare the view to appear onscreen and the device is ready to show the UIView instance before the view is presented. You can override this method to perform custom tasks associated with displaying the view such as to hide fields or disable actions before the view becomes visible.
- viewDidAppear: Notifies the view controller that its view was added to a view hierarchy or window. You can override this, for example, with a waiting screen to show in the view.
- viewWillDisappear: This method is called before the view is actually removed and before any animations are configured. Override this method, for example , to commit editing changes and similar behaviors.
- viewDidDisappear: Notifies the view controller that its view was removed from a view hierarchy. You can override this when you want anything additional to happen in regards to dismissing or hiding the view.
In summary, these stages usually determine the cycle of a view controller. These are methods to track and confirm the order of view events like appearances and disappearances. The view appears on the screen and becomes visible, and then when it’s dismissed it disappears. In contrast to viewDidLoad, the rest of these methods can be called more than once in a view controller life cycle.
In order to learn more about these events or the UIViewController class or related subclasses and objects, please refer to the iOS Developer Library from Apple.