Ios 在Swift中的AppDelegate中显示警报
我尝试下一个代码片段:Ios 在Swift中的AppDelegate中显示警报,ios,swift,uialertcontroller,Ios,Swift,Uialertcontroller,我尝试下一个代码片段: var alert = UIAlertController(title: "Alert", message: "Cannot connect to : \(error!.localizedDescription)", preferredStyle: UIAlertControllerStyle.Alert) alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, ha
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中执行此操作。App Delegate旨在处理来自操作系统的委托功能,而不是实现警报视图之类的功能
如果你想在应用程序开始时显示一个警报视图,我会在你的第一个视图控制器中实现它 我想您是在从ApplicationIDFinishBankingWithOptions:调用该代码段。事实上,我试过了,因为我不得不这么做。问题是:您试图做的是正确的,但是AppDelegate制作和呈现的ViewController即将被放在屏幕上,在此之前,代码段尝试创建一个alertView并放在根本不存在的RootViewController视图之上 我要做的是将它移动到另一个委托调用,该委托调用保证在RootViewController出现后被调用
func applicationDidBecomeActive(application: UIApplication) {
//This method is called when the rootViewController is set and the view.
// And the View controller is ready to get touches or events.
var alert = UIAlertController(title: "Alert", message: "Cannot connect to :", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
self.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
}
但是,我们始终知道AppDelegate的责任。它用于处理应用程序生命周期和应用程序范围内的委托调用和事件。如果把代码放在这里是有意义的,那么就这样做。但是如果您最好将代码放在rootViewController或其他部分上,那么也考虑一下
不管怎样,希望能有帮助。干杯 这就是我现在用来做的
var alertController = UIAlertController(title: "Title", message: "Any message", preferredStyle: .ActionSheet)
var okAction = UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default) {
UIAlertAction in
NSLog("OK Pressed")
}
var cancelAction = UIAlertAction(title: "No", style: UIAlertActionStyle.Cancel) {
UIAlertAction in
NSLog("Cancel Pressed")
}
alertController.addAction(okAction)
alertController.addAction(cancelAction)
self.window?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
Swift 5:
let alert = UIAlertController(title: "Test", message:"Message", preferredStyle: UIAlertController.Style.alert)
// add an action (button)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
// show the alert
self.window?.rootViewController?.present(alert, animated: true, completion: nil)
Swift 3.0或更高版本,可在所有条件下工作,如标签栏、显示视图等
let alert = UIAlertController(title: "Test", message:"Message", preferredStyle: UIAlertControllerStyle.alert)
// add an action (button)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
// show alert
let alertWindow = UIWindow(frame: UIScreen.main.bounds)
alertWindow.rootViewController = UIViewController()
alertWindow.windowLevel = UIWindowLevelAlert + 1;
alertWindow.makeKeyAndVisible()
alertWindow.rootViewController?.present(alertController, animated: true, completion: nil)
您是否尝试过使用
UIApplication.shared.keyWindow?.rootViewController?.present(…)
?我也遇到过类似的问题
我通过在主队列中显示UIAlertController
来修复它
代码如下所示
let alert = UIAlertController(title: "My Title", message: "My Message", preferredStyle: .alert)
let actionYes = UIAlertAction(title: "Yes", style: .default, handler: { action in
print("action yes handler")
})
let actionCancel = UIAlertAction(title: "Cancel", style: .destructive, handler: { action in
print("action cancel handler")
})
alert.addAction(actionYes)
alert.addAction(actionCancel)
DispatchQueue.main.async {
self.window?.rootViewController?.present(alert, animated: true, completion: nil)
}
根据豪尔赫的回答,更新为Swift 4
let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .actionSheet)
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
UIAlertAction in
NSLog("OK Pressed")
}
let cancelAction = UIAlertAction(title: "CANCEL", style: UIAlertActionStyle.cancel) {
UIAlertAction in
NSLog("Cancel Pressed")
}
alertController.addAction(okAction)
alertController.addAction(cancelAction)
self.window?.rootViewController?.present(alertController, animated: true, completion: nil)
在AppDelegate
中,正如错误描述的那样,窗口层次结构尚未创建,因此从那里您无法显示任何内容(至少从didFinishedLaunchingWithOptions
),因此您应该将代码移动到ViewController
@DánielNagy,我看到了,但我必须从AppDelegate中显示它。没有任何解决方案吗?@OrkhanAlizade创建一个ViewController
,将代码放入ViewControllersviewdide
方法中,然后在AppDelegate
中,将ViewController
设置为windowsrootViewController
(并且不要忘记创建窗口本身).@DánielNagy,它起作用了!谢谢大家!@欢迎光临!虽然这个答案可以解决OP的问题,但值得详细说明这是如何实现解决方案的。仅仅发布代码答案可能对OP或未来用户毫无帮助。请详细说明。@GeoffJames done:)如果有多个第一视图控制器发生变化,该怎么办?在swift 4+alertWindow.windowLevel=UIWindow.Level.alert+1最佳答案中~~~~~