简体   繁体   中英

viewDidLoad() and viewDidAppear() called only once?

This is my ViewController (no changes to other files):

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        print("hi from vdl")
    }

    override func viewDidAppear(_ animated: Bool) {
        print("hi from vda")
    }

}
  1. I press the triangle in xCode ("Build and then run the current scheme") and get the output:

hi from vdl
hi from vda

  1. Now I move to the home screen, swipe from the left side of the simulator and swipe the app out. Output:

Message from debugger: Terminated due to signal 9

  1. Now I start the app from the simulator (click on the icon in home screen). App starts but there is no output. Why? I need to rerun the app from xCode to get the output.

When you close the app in simulator, you break the connection with the debugger.

PS You didn't call super in viewDidAppear .

viewDidLoad() and viewDidAppear() work when you get in a VC(ViewController),like present,push,pop,dismiss.

When a VC is presented or pushed to other VC, viewDidLoad() and viewDidAppear() will work.And when you pop/dismiss a VC,the previous VC shows up which make viewDidAppear() work.

But Move to home screen doesn't make effect on the two.

If you want to know when the app will enter the background,use the func applicationDidEnterBackground(_ application: UIApplication) in AppDelegate.swift

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM