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.

  1. loadView
  2. viewDidLoad
  3. viewWillAppear
  4. viewWillLayoutSubviews
  5. viewDidLayoutSubviews
  6. viewDidAppear
  7. viewWillDisappear
  8. viewDidDisappear

ViewLifeCycle

View Management- A view controller uses the following methods to manage its views-

1. loadView

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.

2. viewDidLoad

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.-

  1. Instance variable initialization
  2. XML parsing
  3. Network request
  4. Database access
  5. Heavy object allocation

Event Management to Views- A view controller uses the following methods to manage events to its views-

3. viewWillAppear

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.-

  1. Update navigation bar style
  2. Update status bar style
  3. Update screen data
  4. Orientation handling

4. viewWillLayoutSubviews

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.

5. viewDidLayoutSubviews

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.

6. viewDidAppear

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.-

  1. Start UI animation
  2. Display loader view

 7.  viewWillDisappear

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-

  1. Commit editing changes
  2. Hide keyboard
  3. Revert changes that were made in viewWillAppear method

8. viewDidDisappear

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.-

  1. Remove cache data
  2. Stop services related to view like audio, bonjour service.

Memory Management- A view controller use this method to manage memory to its views-

9.  didReceiveMemoryWarning

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.-

  1. Remove cache
  2. Release instance variables
  3. Release system resources like user’s contacts database

240_F_77959340_hWLiOY93juohUoXqjKzqPB79552nw8XU

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.

Advertisements