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.-
- 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-
- 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.-
- Update navigation bar style
- Update status bar style
- Update screen data
- 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.-
- Start UI animation
- 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-
- Commit editing changes
- Hide keyboard
- 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.-
- Remove cache data
- Stop services related to view like audio, bonjour service.