我是Swift的初学者,我想在我的应用程序一启动就打开一个弹出窗口。为此,我使用以下代码:
import UIKit
class ViewController: UIViewController {
func viewDidAppear(){
if let vc = storyboard?.instantiateViewController(withIdentifier:
"PasswordPopUp") as? PasswordPopUp {
vc.modalPresentationStyle = .overCurrentContext
present(vc, animated: true, completion: nil)
} else {
print("error creating PasswordPopUp")
}
}
}
看起来好像viewDidAppear中的代码根本没有执行。这可能很愚蠢,但是我做错了什么呢?如果能帮上忙我会很感激的。
发布于 2018-06-08 04:32:05
你是说viewDidAppear(_ animated: Bool)
吧。通过说:
func viewDidAppear() {
}
您正在声明一个新方法,而不是viewDidAppear(_ animated: Bool)
。
您应该使用:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// The rest of your code.
}
只需输入viewDidA
,Xcode将为您自动完成它。
发布于 2018-06-08 04:31:58
正确的方法
override func viewDidAppear(_ animated:Bool){
super.viewDidAppear(animated)
if let vc = storyboard?.instantiateViewController(withIdentifier:"PasswordPopUp") as? PasswordPopUp {
vc.modalPresentationStyle = .overCurrentContext
present(vc, animated: true, completion: nil)
} else {
print("error creating PasswordPopUp")
}
}
https://stackoverflow.com/questions/50749716
复制相似问题