Lifecycle of View Controllers for iOS

Ferdous Mahmud Akash

Ferdous Mahmud Akash

· 4 min read
Lifecycle of View Controllers for iOS

Before starting to develop iOS apps, it is crucial to have a clear understanding of the app and view controller lifecycles. Therefore, in this post, I will delve into the view controller lifecycle, including its basics and key concepts.

But some questions will arise in our mind

  • What is View?
  • What are View Controllers?

Let's clear them out.

View

In iOS development, a View is anything that can drown on the screen. TextField, Button, Slider, and screens everything is a view.

View Controller

The view controller is the foundation of an application's internal structure. View Controller is the parent of all the views present on a storyboard.

Every app has at least one view controller, and most apps have several. Each view controller manages a portion of your app’s user interface as well as the interactions between that interface and the underlying data. View controllers also facilitate transitions between different parts of your user interface.

View Controllers Lifecycle

In UIKit, view controllers are responsible for managing the user interface of your app. The view controller lifecycle consists of a series of events that occur as the view controller is created, configured, displayed, and dismissed.

Here are the main phases of the view controller lifecycle:

  • Initialization: When a view controller is created, its init(nibName:bundle:) or init(coder:) method is called, depending on how the view controller is instantiated. This is where you can perform any initial setup that is needed for the view controller.
  • Loading View: After initialization, the view controller's loadView() method is called. This is where you can create the view hierarchy that will be displayed on the screen.
  • View Controller Configuration: Once the view is loaded, the view controller's viewDidLoad() method is called. This is where you can perform any additional setup that is needed for the view controller.
  • Appearance: When a view controller is about to appear on screen, its viewWillAppear(_:) method is called. This is a good place to update the view based on any changes in state that may have occurred.
  • Layout: After the view has appeared, the view controller's viewDidLayoutSubviews() method is called. This is where you can adjust the layout of the view based on its size and position on the screen.
  • Disappearance: When a view controller is about to disappear from the screen, its viewWillDisappear(_:) method is called. This is a good place to save any changes that were made to the view controller's state.
  • Full Disappearance: When viewDidDisappear() method is called after the View Controllers view has been removed from the view hierarchy. Use this method to stop listening to notifications or device sensors.
  • Memory Warning: 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 through didReceiveMemoryWarning() and developers 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.

In conclusion, understanding the View Controller lifecycle is an essential aspect of building successful iOS apps. By knowing the sequence of events that occur during the lifecycle of a view controller, you can ensure that your app behaves as expected and responds appropriately to changes in state. By implementing the lifecycle methods effectively, you can optimize the performance of your app, prevent memory leaks, and improve the overall user experience. Happy coding!

Ferdous Mahmud Akash

About Ferdous Mahmud Akash

Hey there! I'm a self-taught iOS developer from Bangladesh.

I'm also passionate about sharing my knowledge and experience with other aspiring developers through my blog at ferdousmahmud.co

Thank you for visiting my website, and feel free to reach out if you have any questions or just want to say hello!

Copyright © 2021-2024 Ferdous Mahmud Akash. All rights reserved.
Made by Ferdous· Github
LinkTree