UIView Life Cycle In iOS Example

After you create a Xcode project use swift single view app template, it will create a AppDelegate.swift file and a ViewController.swift file.

The AppDelegate.swift file contains this iOS app’s life cycle method which will be triggered when the app’s state is changed. You can read article iOS Application Life Cycle Example to learn more.

The ViewController.swift file contains the app’s main view’s life cycle method which will be triggered when the view state is changed, this article will give you an example to tell you how the view life cycle methods are invoked.

1. iOS App ViewController Life Cycle Methods.

The ViewController.swift file defines a ViewController class which is a sub class of UIKit.UIViewController. It contains 6 life cycle methods, they are listed as below.

  1. viewDidLoad :  This method is triggered after the view controller’s view object is loaded to the app’s view hierarchy, it is executed only once.
  2. viewWillAppear : This method will be invoked when the view object in the view hierarchy will appear in the screen.

  3. viewDidAppear : This method is invoked after the view object appear in the view hierarchy.

  4. viewWillDisappear : This method is triggered before the view object disappear in the view hierarchy.

  5. viewDidDisappear : This method is called after the view object disappear.

  6. didReceiveMemoryWarning : This method is invoked when the view object receive memory warning message which means the iOS memory is not enough.

Please note viewWillDisappear, viewDidDisappear method will only be invoked when you dismiss one UIViewController in the main UIViewController object.

2. UIView Life Cycle Method Example.

The source code in article How To Present / Dismiss UIView Controller Programmatically In Swift has implemented above methods in two UIViewController class, one is the project default UIViewController ( ViewController,swift ), the other is a custom UIViewController ( AnotherViewController.swift ).

READ :   How To Create Xcode Workspace And Add New / Exist Project Into It

There is a button in the default ViewController object’s view, when click this button, it will present AnotherViewController object’s view. Then you can see the AnotherViewController object’s  viewDidLoad, viewWillAppear, viewDidAppear methods be invoked.

There is also a button in the AnotherViewController object’s view, when you click it, it will dismiss the AnotherViewController object’s view, then you can see AnotherViewController object’s viewWillDisappear, viewDidDisappear methods are invoked.

If you simulate iOS device memory warning follow article How To Simulate Memory Warning In Xcode 10 iOS Simulator, then you will find the didReceiveMemoryWarning method is invoked.

When you run the example source code in article How To Present / Dismiss UIView Controller Programmatically In Swift, you will get below output in Xcode debug console.

Another view did load.
Another view will appear.
Another view did appear.
Another view will disappear.
Another view did disappear.
view receive memory warning.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.