在obj-C中,当使用与我的应用程序关联的文件或链接轻触另一个iOS应用程序(邮件附件,Web链接)时。然后我会在openURL或didFinishLaunchingWithOptions
上抓住这个并显示UIAlertView
以确认用户想要导入数据。既然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;
}
但是当我尝试切换到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
}
我收到编译时错误,AppDelegate
没有名为presentViewController
的成员
我看到了一些令人费解的方法让AppDelegate
在StackOverflow上显示UIAlertViewController
,但我希望有一些更简单的东西。
我真正需要做的就是向用户显示一条快速消息,告知他们获得了一些数据并让他们决定要用它做什么。完成后,我的应用程序将继续打开并进入前台(didFinishLaunchingWithOptions
中的类似代码用于冷启动),根据警报选择添加或不添加新数据。
我可以标记一个全局变量,我检查了所有viewWillAppear
func,但由于我有30多个视图,这会有很多重复。
如果您有任何想法,请告诉我。
由于
格雷格
答案 0 :(得分:92)
尝试使用
self.window?.rootViewController?.presentViewController(importAlert, animated: true, completion: nil)
您只需要一个viewController
对象来显示AlertController。
在Swift 4中:
self.window?.rootViewController?.present(importAlert, animated: true, completion: nil)
答案 1 :(得分:20)
使用此代码从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)
})
希望这有帮助!
答案 2 :(得分:2)
我发现的最佳方法如下:
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
}
我添加了一个计时器,因此UIAlertController被解除,因为它没有任何按钮。
感谢:https://stackoverflow.com/a/33128884/6144027关于如何从AppDelegate呈现UIAlertController的精彩回答。
答案 3 :(得分:1)
来自app delegate内部。
window.rootViweController.presentViewController..
。
答案 4 :(得分:0)
Swift 3中接受的答案,以防万一:
self.window?.rootViewController?.present(importAlert, animated: true, completion: nil)