Lifecycle of UIViewController in Swift / Objective c.

UIViewController is the collection of multiple Views. The view controller manages subview layouts, sizing, interface orientations, and functional implementation. It handles instantiated classes from itself like TableviewController, CollectionViewcontroller, TableViewControllert, UISplitViewControlleretc. It also managing content and handles user interaction events. We can create UIViewController views using three different ways Programmatically, Storyboard, and  Xib’s. The view controller class is the object of UIResponder class and it corresponds to the UIResponder chain. ViewController has a lifecycle for managing view contents, the relationship between subviews, sizing, positioning, Navigation operation between view controllers. UIviewcontroller Lifecycle is performed through following predefined standard functions.

Source: www.medium.com

1) ViewDidLoad( ) :

 ViewDidLoad function calls when Viewcontroller actually loaded into memory. Here we can Load subview Xib, Initialise variables and objects, implement one-time work, Call web service, Parse JSON / XML data. ViewDidLoad( ) function is very important because it  defines natural implementation of UIViewController class.

2) ViewWillAppear( ) : 

ViewWillAppear( ) calls after ViewDidLoad( ) and call before view appear. Here we can UI customization, for example, defining subview visibility, Update navigation and status bar behavior, Prepare custom functionality.

3)ViewDidAppear ( ) :

ViewDidAppear( )  call after ViewWillAppear( ) and once view appears . We can make UI customization, UI animation,  Integrate data with UI as table view/collection view reload. 

4) ViewWillDisappear ( ) :

ViewWillDisappear ( ) calls when disappearing view before from view hierarchy (UIWindow).  We can revert changes that we made in ViewWillAppear(), Hide keyboard, and remove subviews.

5) ViewDidDisappear ( ) :

ViewDidDisappear () calls after ViewWillDisappear ( ) and the view is totally disappeared from the view hierarchy. Restore changes Made on the status bar or navigation bar.  Collect and Bind data to move on to the next view controller.

6) viewWillLayoutSubviews() :

viewWillLayoutSubviews() call before any changes happen like positioning and resizing in ViewController. When changes sizing and position changes reflect from parent view to child view viewWillLayoutSubviews() is called.

7) viewDidLayoutSubviews() :

viewDidLayoutSubviews() called after viewWillLayoutSubviews() .we can  Manage orientation constraints like portrait and landscape and resing UIview element in viewDidLayoutSubviews().

8) didReceiveMemoryWarning () :

This method is not implemented directly by the UIviewcontroller class. its called when the amount of memory is very low to perform any task. We can clear cache and deinitialize variables and objects in the didReceiveMemoryWarning method.

Happy coding. Please comment and share the article if you find something useful.

1 thought on “Lifecycle of UIViewController in Swift / Objective c.”

  1. If you need to perform some task every time a view controller comes on screen, then you need the following methods. Keep in mind that in this lifecycle step the view bounds are not final.

    Reply

Leave a Comment