【发布时间】:2015-11-17 17:22:09
【问题描述】:
我试试下一个代码sn-p:
var alert = UIAlertController(title: "Alert", message: "Cannot connect to : \(error!.localizedDescription)", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
self.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
在我的 AppDelegate 中,但它会在控制台中打印下一个错误:
Warning: Attempt to present <UIAlertController: 0x7ff6cd827a30> on <Messenger.WelcomeController: 0x7ff6cb51c940> whose view is not in the window hierarchy!
我该如何解决这个错误?
【问题讨论】:
-
在
AppDelegate中,如错误所述,尚未创建窗口层次结构,因此您无法从那里呈现任何内容(至少来自didFinishedLaunchingWithOptions),因此您应该移动代码到ViewController -
@DánielNagy 我明白了,但我必须从 AppDelegate 展示它。没有解决办法吗?
-
@OrkhanAlizade 创建一个
ViewController,将您的代码放入ViewControllersviewDidAppear方法,并在您的AppDelegate中,将ViewController设置为窗口rootViewController(也不要'不要忘记创建窗口本身)。 -
@DánielNagy 它有效!谢谢!
-
@OrkhanAlizade 欢迎您!
标签: ios swift uialertcontroller