UIView Life Cycle In iOS Example

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.

  1. 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.
  2. 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.
  3. viewWillAppear: This method will be invoked when the view object in the view hierarchy will appear on the screen.
  4. viewDidAppear: This method is invoked after the view object appears in the view hierarchy.
  5. viewWillDisappear: This method is triggered before the view object disappears in the view hierarchy.
  6. viewDidDisappear: This method is called after the view object disappears.
  7. didReceiveMemoryWarning: This method is invoked when the view object receives a memory warning message which means the iOS memory is not enough.
  8. 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.

  1. 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 ).
  2. 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.
  3. 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.
  4. 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.
  5. 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.

References

  1. iOS Application Life Cycle Example
0 0 votes
Article Rating
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x