UIViewController Lifecycle

Sergey Chsherbak
4 min readDec 22, 2021

UIViewController is a fundamental class in the UIKit framework. One of the most important tasks it does is managing the lifecycle of its view. In the present article let’s cover the following questions:
- What are the UIViewController lifecycle methods?
- When and for what each of them is used?

init()

The very first method that is called in the lifecycle of UIViewController is init(). This method is for preparing an instance of a class for use.
Actually, there are two initializers in UIViewController class.

  • init()
  • init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?)

The former initializer will eventually need to call the later one. So let me explain what it does.

This method performs a simple logic. It looks for a .xib (.nib) file. In the case where it finds the .xib file, it associates the controller with it, otherwise, it just does not.

One of the things to remember is that at this stage of the lifecycle, there is no view created and there are not any outlets yet as well. So it means the controller can live happily without a view until there will be a call to a view.

As an example, UITabBarViewController holds references to a specific number of UIViewControllers. However, the view of a controller that is not visible on the screen at the moment will be created only when a user switches to that tab and a call to a view will be made.

loadView()

This method is straightforward. It loads either a view from an associated .xib file or just an empty one.
However, this is the point where you can provide your own custom view. For that, you need to override this method and assign your unique view, that will not be shared across your application with orher controllers, to the view property.

loadViewIfNeeded()

This method is called to load view controller’s view if that has not been yet set.

viewDidLoad()

This method is called after the view controller has loaded its view into memory. This is the great point to perform some additional work on your UI components. However, at this point views’ size may be not the same as it would be after it is shown on the screen. This is why it is not recommended to write any calculation logic that uses width or height of any view.

viewWillAppear(_ animated: Bool)

The method is called just before the view is about to be presented on the screen. If animation is used to present the controller, this method will be called before the animation.

viewWillLayoutSubviews()

This method is called when the controller’s view is about to layout its subviews. As Apple says in the documentation, the default implementation of this method does nothing. But you can override this method to make necessary changes.

viewDidLayoutSubviews()

This is method notifies the controller that its view has just laid out all of its subviews. Same as the previous method, the default implementation of it does nothing, but you can override it to make specific changes.

viewDidAppear(_ animated: Bool)

This method is called just after the view of the controller was shown on the screen. If the animation was used to present the view controller, this method will be called after the animation.

viewWillDisappear(_ animated: Bool)

This method notifies the controller that its view is about to be removed from a view hierarchy. If animation is used to remove the controller, this method will be called before the animation. It is usually used to revert changes that were done in the viewDidAppear method. For example, in the viewDidAppear method, you can change the style of the status bar according to the needs of the specific screen and revert changes in the viewWillDisappear method, just before the view is going to be removed from a view hierarchy.

viewDidDisappear(_ animated: Bool)

This method is called after the controller’s view was removed from a view hierarchy. You can override this method to perform some additional work associated with deleting a view.

didReceiveMemoryWarning()

I usually do not see people adding this method to the UIViewController’s lifecycle. However, I believe, that it is a part of it.
This method is called when the application receives a memory warning. And it receives it when the system determines that the amount of available memory is very low.
You can override this method to perform the release of some memory used by your controller or to save some light data, like a text from a UITextField, and retrieve it later when a user opens an app.

deinit

Since UIViewController is a class it has the deinit method and calls it when the view controller instance was deallocated. Usually, this method is used to remove any observers, for example, from NotificationCenter.

Summary

These methods are part of the UIViewController lifecycle. It’s vital to understand this topic and each of these methods. First of all, because this question is asked in almost every iOS interview. Secondly, it will help you to create better applications.

Thanks for reading!

if you enjoyed this article, be sure to give it a clap so others will find it more easily.

--

--

Sergey Chsherbak

iOS Engineer based in Copenhagen. Writing about Swift for fun. Working with Swift for food.