After you create an Xcode project using a swift single view app template, it will create an 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 the articles listed in the references section 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, which are listed below.
- 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.
- viewWillAppear: This method will be invoked when the view object in the view hierarchy will appear on the screen.
- viewDidAppear: This method is invoked after the view object appears in the view hierarchy.
- viewWillDisappear: This method is triggered before the view object disappears in the view hierarchy.
- viewDidDisappear: This method is called after the view object disappears.
- didReceiveMemoryWarning: This method is invoked when the view object receives a memory warning message which means the iOS memory is not enough.
- Please note the viewWillDisappear, viewDidDisappear methods will only be invoked when you dismiss one UIViewController in the main UIViewController object.
2. UIView Life Cycle Method Example.
- The source code in the article How To Present / Dismiss UIView Controller Programmatically In Swift has implemented the above methods in two UIViewController class, one is the project default UIViewController ( ViewController.swift ), the other is a custom UIViewController ( AnotherViewController.swift ).
- There is a button in the default ViewController object’s view, when clicking this button, it will present the AnotherViewController object’s view. Then you can see the AnotherViewController object’s viewDidLoad, viewWillAppear, viewDidAppear methods are 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 the AnotherViewController object’s viewWillDisappear, viewDidDisappear methods are invoked.
- If you simulate iOS device memory warning follow the 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 the article How To Present / Dismiss UIView Controller Programmatically In Swift, you will get the 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.