View Controller lifecycle

Tram Ho

View controller is a basic component in iOS application. The view controller represents a screen in the application, the normal application has a lot of screens, so more view controllers will be created. UIViewController is a basic view controller class, in addition there are many other types of view controler such as: UITableviewController, UITabbarController, UINavigationController …

The view controller is a container and manages views (including display and content). It is also responsible for responding to the impact of the user on the view.

ViewController life cycle

1. init ()

Is the root function of every class in Swift. So it obviously will run first. However, you do not need to care much because 99.99% is that we do not touch this function.

2.loadview ()

When you are a personality dev, like to code vegetarian and not use interface drafting. This means that your UIViewController is created without using any Storyboard or * .xib files. Then the loadView function will be the first run function (excluding the init function above) to execute.

Its task is to create a view for your ViewController. Of course, that view will have to have frames. After that, all you have to do is run the interface code. You can refer to the following code:

3.viewDidLoad ()

After running loadView, the function viewDidLoad will run. Or if you use Storyboard or * .xib, it will be run first. Some notes are as follows:

  • When the ViewController has been loaded into memory (provided that this ViewController does not already exist in memory), the viewDidLoad function is called.
  • Called only once in the view’s life cycle.
  • Often used to prepare data or initialize default values ​​for objects as well as UI on screen.

4.viewDidUnload ()

This is the opposite of the viewDidLoad function.

  • When your app receives a warning from the system about low memory status, this function will be called
  • At this function will free up unnecessary properties, assign them to free up memory.

It has been dead since iOS 6.0. So now I can not call in the code. However, it is used at low levels.

5.viewWillAppear ()

Before each ViewController you appear it will be called. And it will be called multiple times if your ViewController keeps showing up repeatedly or repeatedly. Examples of types that appear:

  • Is the root of something, like: window
  • Pushed from the navigation
  • App from background is re-enabled
  • In use, drag the Notification Center or Control Center and hide them …

Uses:

  • Know in advance the appearance of the View Controller
  • Prepare data for ViewController or custom to make the application smoother

6.viewDidAppear ()

After each ViewController appears. It with viewWillAppear is a couple. Help you determine the ViewController has finished appearing. Whatever you do, it’s up to you.

7.viewWillDisappear ()

Function will be called when ViewController is about to be hidden / lost / overridden by another ViewController …. The meaning and purpose of use are also different from the above two methods

8.viewDidDisappear ()

When ViewController is gone, this function will be called. It with viewWillDisappear is a couple. The rest are similar to the 3 comrades above.

9. Deinit ()

New to this function, it will be called when ViewController is removed from memory. My job will be to clean the memory here. However, with the current iOS platform, all are designed according to ARC, so you do not need to worry about freeing memory.

10. didReceiveMemoryWarning ()

When your application runs out of RAM and memory is not enough to allocate for the program to work. Then this function will be summoned. At that time:

ViewController itself will have to do memory cleaning

The ViewController is hidden, it will activate its didReceiveMemoryWarning function

Then the ViewController will run the viewDidUnload function, you must assign nil to the interface object, release the variables …

Finally going back to viewDidLoad everything starts over again

There are also a few more functions in the ViewController lifecycle, these are just some common functions and need to know.

Share the news now

Source : Viblo