Latest Post

How to use closure button in swift How to use notificationcenter in swiftui with example Longest common subsequence program in swift

What is View Controller Life cycle?

Ans: View Controllers (VC) are the Controller part of the MVC(Model View Controller) triangle. They are responsible for controlling the view. Every time you want to display more or less significantly complex pieces of UI, you would want to use a View Controller to handle the lifecycle and user interaction callbacks of that UI.

In a nutshell, a View Controller is a simple subclass of UIViewController that has to have a view to draw the UI on. It is either attached to a UIWindow as the root View Controller of that window or managed by a UINavigationController or by another VC or system to be presented to the user.

At the end of the day, when you develop iOS apps there are two main reasons you’d want to use View Controllers:

  • get lifecycle callback for the view that the VC is managing (when the view was loaded, displayed, hidden, etc.)

  • get handy built-in system integrations to present and dismiss VCs using UINavigationController, modal presentation, or parent/child containment API

View Controller lifecycle callbacks:

what is viewcontroller lifecycle in swift

 

  • view controller lifecycle
  • explain view controller lifecycle in ios
  • apple view controller lifecycle
  • extjs view controller lifecycle
  • ui view controller lifecycle
  • objective c view controller lifecycle
  • xamarin ios view controller lifecycle

LoadView()

loadView: you can override this method if you’d like to create the view for your VC manually.

 

ViewDidLoad()

viewDidLoad : this method is called once when your VC’s view was loaded in memory for the first time. Do any additional setup and initializations here. Typically, this is the method where most of your custom view initialization and autolayout setup will go. Also, start your services and other async data-related stuff here.

 

ViewWillAppear()

viewWillAppear : this method is called when the view is about to appear on the screen. It will be called after viewDidLoad and ev- ery subsequent time after view disappears from screen and then appears again. For example, when you present a view in a navbar it will call viewDidLoad and then viewWillAppear/viewDidAppear for that VC.

Later, if you push a new VC on top of it, viewWillDisappear and viewDidDisappear will be called because it’s no longer the fore- ground/top VC. Later still, if the user taps the Back button, viewWill- Appear and viewDidAppear for that first VC will be called because it becomes the top VC again. Use this method to do final UI customiza- tions, hook up UI observers, etc.

 

ViewWillLayoutSubviews()

viewWillLayoutSubviews is called right before layout Subviews() in underlying UIView for that VC. It is rarely used to adjust your view positioning.

 

ViewDidLayoutSubview()

viewDidLayoutSubviews()is called right after layout Subviews() in underlying UIView for that VC. It is rarely used to adjust your view positioning.

 

viewDidAppear()

viewDidAppear :this method is called right after the view as shown on the screen and follows a viewWillAppear call.

 

viewWillDisppear()

viewWillDisappear is called when the view is about to become “hidden” i.e. not the top view controller presented to the user (see example above).

 

viewDidDisappear()

viewDidDisappear is called after viewWillDisappear and indicates that the view is “hidden”.

 

didReceiveMemoryWarning()

didReceiveMemoryWarning is called when the system is low on memory and needs to release additional resources. Deallocate as much as you can here. Don’t go crazy about it, though, because nowadays phones are so powerful that memory warnings rarely happen.

 

Additionally, View Controller, just like Views, can be initialized either pro- grammatically in code using init… constructor/initializer methods or loaded from a storyboard/xib file. In the former case, one of the initializer methods will be called and in the latter it will be via -initWithCoder.

 

Red flag:you simply have to know this stuff to be able to develop iOS apps.

 

 

 

Leave a Reply

Your email address will not be published.