Simple App Delegate方法显示UIAlertController(在Swift中) [英] Simple App Delegate method to show an UIAlertController (in Swift)
问题描述
在obj-C中,当使用与我的应用程序关联的文件或链接轻触另一个iOS应用程序(邮件附件,Web链接)时。然后我会在openURL或 didFinishLaunchingWithOptions
上捕获它,并显示 UIAlertView
以确认用户想要导入数据。现在 UIAlertView
被折旧我试图做同样的事情,但不确定最好的方法吗?
当我的应用从其他应用收到数据时,我无法显示简单的提醒。这个代码在Objective-C中运行良好,带有 UIAlertView
:
- (BOOL)应用程序:(UIApplication *)应用程序openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
if(url)
{
self.URLString = [url absoluteString];
NSString * message = @收到数据交换请求。是否要导入它?;
importAlert = [[UIAlertView alloc] initWithTitle:@Data Received消息:消息委托:self cancelButtonTitle:@CancelotherButtonTitles:@OK,nil];
[importAlert show];
}
返回YES;
}
但是当我尝试切换到 UIAlertViewController
和Swift我似乎无法找到一种显示消息的简单方法:
func application(application :UIApplication,openURL url:NSURL,sourceApplication:String?,annotation:AnyObject?) - > Bool {
让URLString:String = url.absoluteString!
let message:String =收到的数据。你要导入它吗?
var importAlert:UIAlertController = UIAlertController(标题:数据已接收,消息:消息,preferredStyle:UIAlertControllerStyle.Alert)
importAlert.addAction(UIAlertAction(标题:取消,样式: .Cancel,handler:nil))
importAlert.addAction(UIAlertAction(title:Ok,style:.Default,handler:
{action in
switch action.style {
case。默认:
println(default)
case .Cancel:
println(cancel)
case .Destructive:
println(destructive )
}
}))
self.presentViewController(importAlert,animated:true,completion:nil)
return true
}
我收到编译时错误 AppDelegate
没有成员名为 presentViewController
我见过一些复杂的方法来获取 AppDelegate
显示<$ c StackOverflow上的$ c> UIAlertViewController 但我希望有一些更简单的东西。
我真正需要做的就是向用户展示快速消息,他们得到了一些数据,并让他们决定他们想用它做什么。一旦完成,我的应用程序将继续打开并进入前台(类似的代码在 didFinishLaunchingWithOptions
用于冷启动),根据警报选择添加或不添加新数据。 / p>
我可以标记一个全局变量,我在我的所有 viewWillAppear
func中检查但是这会有很多重复我有30多个观点。
如果您有任何想法,请告诉我。
谢谢
Greg
尝试使用
self.window?.rootViewController?.presentViewController(importAlert,animated:true,completion:nil)
您只需要一个 viewController
对象来呈现AlertController。
在Swift 4中:
self.window?.rootViewController?.present(importAlert,animated:true,completion:nil )
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?
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
:
- (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:
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
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.
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.
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.
Let me know if you have any ideas.
Thanks
Greg
Try using
self.window?.rootViewController?.presentViewController(importAlert, animated: true, completion: nil)
All you need is a viewController
object to present the AlertController from.
In Swift 4:
self.window?.rootViewController?.present(importAlert, animated: true, completion: nil)
这篇关于Simple App Delegate方法显示UIAlertController(在Swift中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!