View Controller Life Cycle in iOS

UIViewController– A view controller manages a set of views and makes your app’s user interface. It coordinates with model objects and other controller objects. Basically, it plays a combine role for both view objects and controller objects. Each view controller shows it’s own views to display the app content. The views are automatically loaded when the view property of view controller is accessed in the app.

iOS provides several in built view controller classes for supporting standard user interfaces. These classes are UITableViewController, UINavigationController, UITabBarController etc.

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

  • 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 manually. If you use Interface Builder to create view controller’s views, you must not override this method.
  • viewDidLoad– It is automatically called  when the view controller loaded completely in the memory. 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-

  • viewWillAppear– It is called when the view controller’s view is about to be added to the view hierarchy. 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
  • viewDidAppear– It is called when the 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
  • 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
  • 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-

  • 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

Leave a Reply