ios 在 Swift 中的 AppDelegate 中显示警报
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32179710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Show alert in AppDelegate in Swift
提问by Orkhan Alizade
I try the next code snippet:
我尝试下一个代码片段:
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)
in my AppDelegate, but it prints me the next error in console:
在我的 AppDelegate 中,但它在控制台中向我打印了下一个错误:
Warning: Attempt to present <UIAlertController: 0x7ff6cd827a30> on <Messenger.WelcomeController: 0x7ff6cb51c940> whose view is not in the window hierarchy!
How can I fix this error?
我该如何解决这个错误?
回答by Jorge L Hernandez
This is what i'm using now to do that.
这就是我现在用来做的。
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)
回答by Ahmed Safadi
SWIFT 3
斯威夫特 3
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 the alert
self.window?.rootViewController?.present(alert, animated: true, completion: nil)
回答by Chris Herbst
As per Jorge's answer, updated for Swift 4
根据 Jorge 的回答,针对 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)
回答by emraz
Swift 3.0 or above, Working in all condition , like in case of tab bar, in case of presented view etc ..
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)
回答by Rashesh Bosamiya
I had the similar problem.
我有类似的问题。
I have fixed it by presenting UIAlertController
in Main Queue
.
我已经通过UIAlertController
在Main Queue
.
Code Looks like following.
代码如下所示。
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)
}
回答by kandelvijaya
I suppose you are calling that code snippet from the applicationDidFinishLunchingWithOptions: . I tried it as a matter of fact because i had to. The thing is: what you are trying to do is correct but the ViewController that the AppDelegate makes and presents is about to be put on screen and before that, the code snippet tries to create a alertView and put in on top of non existent View of the RootViewController.
我想您是从 applicationDidFinishLunchingWithOptions: 调用该代码片段。事实上,我尝试过,因为我不得不这样做。问题是:您尝试做的事情是正确的,但是 AppDelegate 制作和呈现的 ViewController 即将放在屏幕上,在此之前,代码片段尝试创建一个 alertView 并放在不存在的 View 之上根视图控制器。
What i would do is move it to another delegate call which is guaranteed to be called after the RootViewController is presented.
我要做的是将它移动到另一个委托调用,该调用保证在 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)
}
But as always know the responsibility of the AppDelegate. It is to handle the application lifecycle and application wide delegate calls and events. If putting code here makes sense, then do it. But if you will be better off putting the code on the rootViewController or other parts then think about it too.
但一如既往地知道 AppDelegate 的责任。它用于处理应用程序生命周期和应用程序范围的委托调用和事件。如果把代码放在这里有意义,那就去做吧。但是,如果将代码放在 rootViewController 或其他部分会更好,那么也请考虑一下。
Anyway, hope it helps. Cheers!
无论如何,希望它有所帮助。干杯!
回答by mmika1000
Have you tried using UIApplication.shared.keyWindow?.rootViewController?.present(...)
?
你试过使用UIApplication.shared.keyWindow?.rootViewController?.present(...)
吗?
回答by Swinny89
I would suggest NOT doing this in the AppDelegate. The App Delegate it intended to handle Delegate functions from the OS rather than implementing things like alert views.
我建议不要在 AppDelegate 中这样做。App Delegate 它旨在处理来自操作系统的委托功能,而不是实现诸如警报视图之类的东西。
If you are wanting to present an alert view here to be shown at the start of the app I would do this by implementing it in your first view controller.
如果您想在应用程序开始时在此处显示警报视图,我会通过在您的第一个视图控制器中实现它来实现。