iOS View Controller Life Cycle

Ekramul Hoque
Good Morning Swift
Published in
4 min readMay 20, 2018

Introduction:

It’s important to understand the Apps Life cycle and View Controller life Cycle before starting to develop iOS Apps. So today I will discuss the View controllers life cycle and a few basics of the View controller.

What you will learn from this tutorial :

What is View Controller?

Why View Controller important.

How to view Controller work.

View controller life cycle.

There is three-way to create UI (Which is attached with a viewController class ).

  1. From the .xib
  2. From Code
  3. By Storyboard

View controller Life Cycle :

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 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 .You Can do Some common task in this method :

1.Network call which need Once.

2.User Interface

3.Others Task Those are Need to do Once

Note: This Method Call before the bound are defined and rotation happen.So its Risky to work view size in this method.

viewWillAppear:

This method is called every time before the view is visible to and before any animation is configured. In this method view has bound but orientation is not set yet. 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.

viewWillLayoutSubviews:

It doesn’t do Anything by default. When a view’s bounds change, the view adjusts the position of its subviews. The view controller can override this method to make changes before the view lays out its subviews.

viewDidLayoutSubviews:

This method is called after the viewController has been adjusting to its subview following a change on its bound. Add code here if you want to make changes to subviews after they have been set.

viewDidAppear:

This method is called after the view present on the screen. Usually, 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 the view is removed from the view hierarchy. The View is Still on the view hierarchy but not removed yet. any unload animations haven’t been configured yet. Add code here to handle timers, 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 VC’s view has been removed from the view hierarchy. Use this method to stop listening for notifications or device sensors.

deinit:

Before a view controller is removed from memory, it gets deinitialized. You usually override deinit() to clean resources that the view controller has allocated that are not freed by ARC. Keep in mind that just because a VC is no longer visible, doesn’t mean that it has been deallocated. Container view controllers such as NavigationController can keep their VCs available in memory. Keep in mind that even though a VC is off-screen, if it is still in memory, it still works normally and can receive notifications.

didReceiveMemoryWarning()

When the memory starts to fill up, iOS does not automatically move data from memory to its limited hard disk space. It does, however, issue this warning and YOU (I mean YOU) are responsible for clearing 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.

--

--