本质上,当 View 仅在应用程序首次加载时第一次出现时,我只想打印一次以下“hello”消息。 The ViewController can be shown again when a tab in a tab controller triggers it, but it should not print the message after the very first time.返回到 ViewController 不应触发此“hello”打印。
下面是我已经看腻了,但是好像不行?
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if self.isBeingPresented || self.isMovingToParent {
print("hello")
}
}
最佳答案
最稳定的方式是
var once = true
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if once {
print("hello")
once = false
}
}
作为self.isBeingPresented || self.isMovingToParen
可以根据您的使用位置给出意想不到的结果
关于ios - 如何仅在第一次显示 View Controller 时触发 viewDidAppear 中的操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56873849/