DEV Community

Ajithmadhan
Ajithmadhan

Posted on

ViewController lifecycle in iOS

iOS View Controller Life 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 combined 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 the view controller is accessed in the app.

A View Controller provides multiple methods for manage the views.

View controller Life Cycle :

View life cycle

Lifecycle methods:

loadView

This method use when view Controller creates from code . Its good not to do anything on this method.If view Controller made from .xib or storyboard.
What to do in View Load : loadView( ) is a method managed by the viewController. The viewController calls it when its current view is nil. loadView( ) basically takes a view (that you create) and sets it to the viewController’s view (superview).

viewDidLoad

This Method is loaded once in view controller life cycle .Its Called When all the view are loaded.
what to do in viewDidLoad:you can do some common tasks like Network call which need Once,User Interface,Others Task Those are Need to do Once

Note:
This method is called before the bounds are defined.

ViewWillAppear

This method is called every time a view is a appeared on screen.
What to do in ViewWillAppear:You can override this method to perform custom tasks associated with displaying the view such as hiding fields or disabling actions before the view becomes visible.

ViewDidAppear

This method is called after the view present on the screen.
What to do in ViewDidAppear: save data to core data or start animation or start playing a video or a sound, or to start collecting data from the network This type of task good for this method.

ViewWillDisappear:

This method is called before view is removed from view hierarchy (the view is still in view hierarchy).
What to do in ViewWillDisappear:hide the keyboard, canceling network requests, revert any changes to the parent UI. Also, this is an ideal place to save the state.

ViewDidDisappear:

This method is called after the view is removed from view hierarchy.
What to do in ViewDidDisappear:You can stop listening for notifications or device sensors.

didReceiveMemoryWarning:

When the memory starts to fill up,iOS does not manages the app's memory status, you are responsible to clear up some objects out of memory.Be aware that if the memory of your app goes over a certain threshold, iOS will shut down your app. Unfortunately, this will look like a crash to the end-user.

viewWillTransition(to:with:)

When the interface orientation changes, UIKit calls this method on the window’s root view controller before the size changes are about to be made. The root view controller then notifies its child view controllers, propagating the message throughout the view controller hierarchy. The parameter to contains the new CGSize size of the container’s view and the parameter with contains a UIViewControllerTransitionCoordinator coordinator, an enum that describes the new orientation.

Top comments (0)