What is the UIViewController Lifecycle?

The UIViewController lifecycle is something I had to learn early on in my software career. It’s essential to understand this properly in order to update views correctly. I’ll break it down step by step.

What is the UIViewController Lifecycle?

  • init
  • viewDidLoad
  • viewWillAppear
  • viewDidAppear
  • viewWillDisappear
  • viewDidDisappear

Let’s dive a little deeper into each of the stages of a view controller.

uiviewcontroller

Initialization of the UIViewController

This is how all view controllers start off. They are initialized, typically with code like so:

let newViewController = UIViewController()

This method will also be called if you decide to create the view controller in your storyboard.

Initialization for view controllers is only called once, just like init methods for other objects. You can create a custom init method for your view controller and set up some variables. However, just because a view controller is initialized, it does not mean it will be shown.

Another view controller or a navigation controller must push this view controller in order for it to be shown.

It’s best to save setup and other actions for the view controllers viewDidLoad() function.

viewDidLoad

viewDidLoad is a pretty important method. I’ve overridden this function in most of my view controllers.

The method gets called after your view controller has loaded the view hierarchy into memory. It gets called whether the view loaded from a nib or created in code.

I’d say I override this method maybe in 90%+ of the view controllers I create.

This is a good place to set up any dynamic views that depend on any kind of data. It’s where you can set up custom label text, change the color based on user behavior or hide views based on the app state.

Here’s an example of a view controllers viewDidLoad():

override func viewDidLoad() {
  someLabel.text = "Updated Text!"
  someButton.backgroundColor = UIColor.red
  if shouldHideView {
    someView.isHidden = true
  }
}

Keep in mind that this method will only be called once when the view controller is loaded into memory. If you need a method thats called every time the view appears on screen, you should use viewWillAppear()

viewWillAppear

While viewDidLoad() is only called once, viewWillAppear() is called every time the view will appear on the screen.

This can be useful for views that need to be updated every time they’re scrolled to. For example, the Now Playing section in a music app or any view that is updated when it is scrolled to.

You could also use this to update the status bar when a certain view is appearing or navigated back to.

Here’s an example of a viewWillAppear():

override func viewWillAppear(_ animated: Bool) {
  someTextView.becomeFirstResponder()
  setupNavBar()
  updateSomeView()
  super.viewWillAppear(animated)
}

viewDidAppear

View did appear is called, as the name suggests, when the view has appeared on the users screen.

This is a good place to “lazy-load” any other elements onto your screen. If your user interface is very complicated, you don’t want to stall or cause a delay in the previous mentioned methods.

It’s better to load something onto the screen and then call some methods to update the views for some large applications.

This is also a create place to start any animations.

Here’s an example of an override of viewDidAppear()

override func viewDidAppear(_ animated: Bool) {
  super.viewDidAppear(animated)
  lazyLoad()
  startSomeTimer()
}

viewWillDisappear()

This is called right before a view is removed from the user’s screen. It happens before any animations are started.

This is a good place to save the state of your view. For example, if your view has some text editing, you can save the users input here. That way if they come back to the view, you can be sure to still have their work saved.

You could also use this function to revert any changes you created in viewDidAppear as well. Undo status bar changes or other view stack changes.

Here’s an example of a view will disappear method:

override func viewWillDisappear(_ animated: Bool) {
  super.viewWillDisappear(true)
  undoNavBarChanges()
  saveUserInput()
}

viewDidDisappear()

This view gets called when the view is fully removed from the screen. You can override this to clean up some stuff associated with the view being removed.

Honestly I don’t use this method that often and its use cases are limited.

Here’s an example of a viewDidDisappear method:

override func viewDidDisappear(_ animated: Bool) {
  super.viewDidDisappear(animated)
  unsubscribeToUpdates()
}
If you liked this post and want to learn more, check out The Complete iOS Developer Bootcamp. Speed up your learning curve - hundreds of students have already joined. Thanks for reading!

Eddy Chung

I teach iOS development on ZeroToAppStore.com.

Similar Posts