ios 显示 UIAlertController 的简单 App Delegate 方法(在 Swift 中)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26627511/
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
Simple App Delegate method to show an UIAlertController (in Swift)
提问by Greg Robertson
In obj-C when another iOS app (mail attachment, web link) was tapped with a file or link associated with my app. I would then catch this on openURL or didFinishLaunchingWithOptions
and show a UIAlertView
to confirm the user wants to import the data. Now that UIAlertView
is depreciated I am trying to do the same thing but not really sure about the best way to do this?
在 obj-C 中,当另一个 iOS 应用程序(邮件附件、Web 链接)与我的应用程序关联的文件或链接被点击时。然后我会在 openURL 上捕获它,或者didFinishLaunchingWithOptions
显示一个UIAlertView
以确认用户想要导入数据。现在UIAlertView
已经折旧了,我正在尝试做同样的事情,但不确定最好的方法是什么?
I am having trouble showing a simple alert when my App receives data from another app. This code worked fine in Objective-C with a UIAlertView
:
当我的应用程序从另一个应用程序接收数据时,我无法显示简单的警报。这段代码在 Objective-C 中运行良好,带有UIAlertView
:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
if (url)
{
self.URLString = [url absoluteString];
NSString *message = @"Received a data exchange request. Would you like to import it?";
importAlert = [[UIAlertView alloc] initWithTitle:@"Data Received" message:message delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
[importAlert show];
}
return YES;
}
But when I try and switch to UIAlertViewController
and Swift I can't seem to find a simple way to display the message:
但是当我尝试切换到UIAlertViewController
Swift 时,我似乎找不到一种简单的方法来显示消息:
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
let URLString: String = url.absoluteString!
let message: String = "Received data. Would you like to import it?"
var importAlert: UIAlertController = UIAlertController(title: "Data Received", message: message, preferredStyle: UIAlertControllerStyle.Alert)
importAlert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
importAlert.addAction(UIAlertAction(title: "Ok", style: .Default, handler:
{ action in
switch action.style {
case .Default:
println("default")
case .Cancel:
println("cancel")
case .Destructive:
println("destructive")
}
}))
self.presentViewController(importAlert, animated: true, completion: nil)
return true
}
I get a compile time error that AppDelegate
does not have a member named presentViewController
我收到一个AppDelegate
没有名为成员的编译时错误presentViewController
I have seen some convoluted methods to get the AppDelegate
to display an UIAlertViewController
on StackOverflow but I was hoping there was something a little simpler.
我已经看到一些令人费解的方法来在 StackOverflowAppDelegate
上显示一个UIAlertViewController
,但我希望有一些更简单的方法。
All I really need to do is show the user a quick message that they got some data and have them decide what they want to do with it. Once were done my app will continue to open and come to foreground (similar code in didFinishLaunchingWithOptions
for cold start) with either the new data added or not based on the alert selection.
我真正需要做的就是向用户显示一条快速消息,告诉他们他们获得了一些数据,并让他们决定要用它做什么。完成后,我的应用程序将继续打开并进入前台(didFinishLaunchingWithOptions
冷启动中的类似代码),并根据警报选择添加或不添加新数据。
I could flag a global variable that I check in all my viewWillAppear
func but this would be a lot of duplication since I have 30+ views.
我可以标记一个全局变量,我将在所有viewWillAppear
函数中检查该变量,但这将是很多重复,因为我有 30 多个视图。
Let me know if you have any ideas.
如果您有任何想法,请告诉我。
Thanks
谢谢
Greg
格雷格
回答by ZeMoon
Try using
尝试使用
self.window?.rootViewController?.presentViewController(importAlert, animated: true, completion: nil)
All you need is a viewController
object to present the AlertController from.
您所需要的只是一个viewController
用于显示 AlertController的对象。
In Swift 4:
在 Swift 4 中:
self.window?.rootViewController?.present(importAlert, animated: true, completion: nil)
回答by Aditya Gaonkar
Use this code to launch alertCV from appdelegate
使用此代码从 appdelegate 启动 alertCV
dispatch_async(dispatch_get_main_queue(), {
let importantAlert: UIAlertController = UIAlertController(title: "Action Sheet", message: "Hello I was presented from appdelegate ;)", preferredStyle: .ActionSheet)
self.window?.rootViewController?.presentViewController(importantAlert, animated: true, completion: nil)
})
Hope this helps!
希望这可以帮助!
回答by Marie Amida
The best way I've found is the following:
我发现的最好方法如下:
let importantAlert: UIAlertController = UIAlertController(title: "Action Sheet", message: "Hello I was presented from appdelegate ;)", preferredStyle: .ActionSheet) //.Alert .ActionSheet
var hostVC = UIApplication.sharedApplication().keyWindow?.rootViewController
while let next = hostVC?.presentedViewController {
hostVC = next
}
hostVC?.presentViewController(importantAlert, animated: true, completion: nil)
let delay = 1.5 * Double(NSEC_PER_SEC)
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(time, dispatch_get_main_queue()) {
importantAlert.dismissViewControllerAnimated(true, completion: nil)
return
}
I've added a timer so the UIAlertController is dismissed since it doesn't have any buttons in it.
我添加了一个计时器,因此 UIAlertController 被关闭,因为它没有任何按钮。
Thanks to: https://stackoverflow.com/a/33128884/6144027Brilliant answer on how to present a UIAlertController from AppDelegate.
感谢:https: //stackoverflow.com/a/33128884/6144027 关于如何从 AppDelegate 呈现 UIAlertController 的精彩回答。
回答by Daniel T.
From inside the app delegate.
从应用程序委托内部。
window.rootViweController.presentViewController..
.
window.rootViweController.presentViewController..
.
回答by user1333394
The accepted answer in Swift 3 in case it helps anyone:
Swift 3 中接受的答案,以防它对任何人有帮助:
self.window?.rootViewController?.present(importAlert, animated: true, completion: nil)