What is Lifecycle of ViewController? — iOS Development with Swift

Mammadowr
4 min readOct 2, 2022

--

After a long time, I decided to come back with an article about one of the most important topic for every iOS Developer.

Before we start, if you did not check yet, check my other articles:

If you are ready, let’s start. 🙂

It is very important to understand and use the view controller life cycle correctly when developing iOS applications. As the name suggests, the lifecycle defines what the view controller will do during its life. Many operations such as loading, adding or deleting the view controller to the view hierarchy are completed in the lifecycle. The life of a view controller starts when it is initialised like every class. Then the view of the view controller is created through the storyboard. When these are completed, the viewDidLoad method is called.

loadView()
- If you created the view using storyboard, you don’t need to use this method. However, if you created the view by writing code, you can use this method instead of viewDidLoad().

viewDidLoad()
works when the view of the view controller is created and loaded from the storyboard. At this point, it is guaranteed that the outlets we created are created. Customisations related to the view controller (IBOutlet) are usually done here. The only point you should pay attention to here is that the viewDidLoad() method is called only once.

viewWillAppear() This method is called just before the view of the view controller is added to the view hierarchy of the application. However, we reserve this method for operations that are not very costly and that we need to perform just before the view controller is shown. The difference from viewDidLoad() is that it is called immediately before it and is called every time the view controller appears.

viewDidAppear() is called immediately after the view of the view controller is added to the view hierarchy of the application. It is a convenient method for calling your animation or pulling data, if any. Again, like viewWillAppear(), it is called every time the screen is shown.

viewWillDisappear() is called just before the view of the view controller is deleted from the view hierarchy of the application. In other words, it is called just before the view controller disappears. It can be used to hide the keyboard or save any data in the view controller.

viewDidDisappear() is called immediately after the view of the view controller is deleted from the view hierarchy of the application.
viewDidLoad() is called when the view is finished loading, and loadView() is called when the view starts loading.

We have completed our definitions, but you may be wondering where we should use them in real life. I will try to illustrate the usage scenarios a little more with the following examples.

viewDidLoad() is the most suitable place for customisations. For example, we can change the text of our label here.

viewWillAppear() can be used for data that we need to prepare in advance. We can use it to update the user interface with the data we pull from an API.

viewDidAppear() Assume that our application has a Login screen, we can use it here for operations such as showing the Login screen if the user has not logged in before.

viewWillDisappear() For example, we use a database and we can use this place to save the changes we have made in this database through the view controller.

viewDidDisappear() for example, if we want to show or hide a view when the view controller disappears, we can use it here.

Basically that is all from my side. If you like my articles, if you think that they are useful, you can click on “Follow” button and share articles to reach more people. :)

--

--

Mammadowr

I speak 6 human and 5 programming languages. 🥳  iOS Developer. To support me: https://www.buymeacoffee.com/mammadowr8 ☕️