UIAlertController 代码运行,但不显示警报 [英] UIAlertController code runs, but doesn't present an alert
问题描述
我正在尝试使用 swift 制作应用内弹出式警报,但遇到了一个我一无所知的错误.
I am trying to make an in-app popup alert using swift and I have run into an error that I know nothing about.
这是我用来显示警报的代码:
Here is the code I use to present my alert:
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!")
我返回的错误是这样的:
The error I'm getting back says this:
Warning: Attempt to present <UIAlertController: 0x7b89a950> on <MyApp.RootClientViewController: 0x7aea8fb0> whose view is not in the window hierarchy!
紧随其后的是打印声明,说明显示欢迎警报!
.
This is immediately followed by a printed statements stating welcome alert displayed!
.
所以我的代码肯定正在运行,但由于某种原因,它不会显示警报...
So my code is certainly running, but for some reason, it won't display the alert...
我做错了什么?
推荐答案
错误信息告诉你答案:视图不在窗口层次结构中!" 表示 self.view<调用时/code> 不在屏幕上(从技术上讲,这意味着
UIApplication.sharedApplication().keyWindow
不是 self.view
的祖先).
The error message tells you the answer: "view is not in the window hierarchy!" means self.view
is not on screen when the call is made (technically it means UIApplication.sharedApplication().keyWindow
is not an ancestor of self.view
).
通常在 viewDidLoad()
或 viewWillAppear(animated: Bool)
中呈现视图控制器时会发生这种情况.等待 viewDidAppear(animated: Bool)
,从 UIApplication.sharedApplication().delegate.window.rootViewController
出现或从 UIApplication.sharedApplication().keyWindow 出现.rootViewController
.
Usually this happens when presenting a view controller in viewDidLoad()
or viewWillAppear(animated: Bool)
. Wait for viewDidAppear(animated: Bool)
, present from UIApplication.sharedApplication().delegate.window.rootViewController
or present from UIApplication.sharedApplication().keyWindow.rootViewController
.
这篇关于UIAlertController 代码运行,但不显示警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!