Lifecycle of iOS ViewController

The lifecycle of the ViewController refers to the lifecycle of the views it controls. When the state of a view changes, the ViewController will automatically call a series of methods in response to the change. The lifecycle of the ViewController is shown in Figure 10.4.

Figure 10.4. Lifecycle of ViewController

Each method is used as follows:

  • init() initializes relevant, critical data.

  • loadView() initializes the view. This method should not be called directly, but automatically by the system.

  • viewDidLoad() indicates that the view is loaded, but not yet been displayed on the screen. By overriding this method, you can perform additional initialization on views, such as removing some views, modifying constraints, loading data, etc.

  • viewWillAppear() indicates that the view is about to be displayed on the screen. You may use this method to change the orientation or style of the status bar.

  • viewDidApper() indicates that the view has been displayed on the screen. You may use this method to modify how the view is presented.

  • viewWillDisappear() indicates that the view is about to disappear, be covered, or be hidden.

  • viewDidDisappear() indicates that the view has disappeared, been covered, or been hidden.