我正在尝试使用 swift 制作应用内弹出警报,但我遇到了一个我一无所知的错误。
这是我用来显示警报的代码:
let welcomeAlert = UIAlertController(title: "Welcome!", message: “message here”, preferredStyle: UIAlertControllerStyle.Alert)
welcomeAlert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(welcomeAlert, animated: true, completion: nil)
println("welcome alert displayed!")
我返回的错误是这样说的:
Warning: Attempt to present <UIAlertController: 0x7b89a950> on <MyApp.RootClientViewController: 0x7aea8fb0> whose view is not in the window hierarchy!
紧随其后的是打印语句,说明显示欢迎警报!。
所以我的代码肯定在运行,但出于某种原因,它不会显示警报...
我做错了什么?
请您参考如下方法:
错误消息告诉你答案:“view is not in the window hierarchy!” 意思是调用时 self.view 不在屏幕上(技术上这意味着 UIApplication.sharedApplication().keyWindow 不是 self.view 的祖先)。
通常在 viewDidLoad() 或 viewWillAppear(animated: Bool) 中呈现 View Controller 时会发生这种情况。等待 viewDidAppear(animated: Bool),从 UIApplication.sharedApplication().delegate.window.rootViewController 出现或从 UIApplication.sharedApplication().keyWindow 出现。 Root View Controller 。
