简体   繁体   English

viewDidLoad() 和 viewDidAppear() 只调用一次?

[英]viewDidLoad() and viewDidAppear() called only once?

This is my ViewController (no changes to other files):这是我的 ViewController(对其他文件没有更改):

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:我按下 xCode 中的三角形(“构建然后运行当前方案”)并得到 output:

hi from vdl来自vdl的你好
hi from vda你好来自vda

  1. Now I move to the home screen, swipe from the left side of the simulator and swipe the app out.现在我移动到主屏幕,从模拟器的左侧滑动并滑动应用程序。 Output: Output:

Message from debugger: Terminated due to signal 9来自调试器的消息:由于信号 9 而终止

  1. Now I start the app from the simulator (click on the icon in home screen).现在我从模拟器启动应用程序(单击主屏幕中的图标)。 App starts but there is no output.应用程序启动但没有 output。 Why?为什么? I need to rerun the app from xCode to get the output.我需要从 xCode 重新运行应用程序以获取 output。

When you close the app in simulator, you break the connection with the debugger.当您在模拟器中关闭应用程序时,您会断开与调试器的连接。

PS You didn't call super in viewDidAppear . PS您没有在viewDidAppear中调用super

viewDidLoad() and viewDidAppear() work when you get in a VC(ViewController),like present,push,pop,dismiss. viewDidLoad()viewDidAppear()在你进入 VC(ViewController) 时工作,比如 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.当一个 VC 被呈现或推送到其他 VC 时, viewDidLoad()viewDidAppear()将起作用。当你弹出/关闭一个 VC 时,前一个 VC 会出现,这使得viewDidAppear()起作用。

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如果您想知道应用程序何时进入后台,请使用AppDelegate.swift中的func applicationDidEnterBackground(_ application: UIApplication)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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