我似乎无法在启动视图时弹出警报视图。代码如下。
import UIKit
class StartController: UIViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
self.view.backgroundColor = UIColor.white;
startTest();
}
func startTest()
{
let alerta = UIAlertController(title: "Invalid Test", message: "Testing alert controller", preferredStyle: UIAlertControllerStyle.alert);
alerta.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil));
self.present(alerta, animated: true, completion: nil);
}
}
答案 0 :(得分:3)
问题是在viewDidLoad中,视图层次结构未完全设置。如果使用viewDidAppear,则设置层次结构。
如果您真的想在viewDidLoad中调用此警报,可以通过将演示文稿调用包装在此GCD块中来导致轻微延迟
DispatchQueue.main.async {
// Run UI Updates or call completion block
startTest()
}
或在
中使用override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
startTest()
}
答案 1 :(得分:1)
在startTest()
方法中调用viewDidAppear
。它对我有用。
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
startTest()
}
func startTest()
{
let alerta = UIAlertController(title: "Invalid Test", message: "Testing alert controller", preferredStyle: UIAlertControllerStyle.alert);
alerta.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil));
self.present(alerta, animated: true, completion: nil);
}
答案 2 :(得分:0)
试着把它放在viewDidAppear:
方法
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
startTest()
}