Gre*_*son 38 objective-c uialertview ios swift
在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;
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试切换到UIAlertViewControllerSwift时,我似乎找不到一种简单的方法来显示消息:
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
}
Run Code Online (Sandbox Code Playgroud)
我得到一个AppDelegate没有名为成员的编译时错误presentViewController
我已经看到一些复杂的方法来AppDelegate显示UIAlertViewControllerStackOverflow,但我希望有一些更简单的东西.
我真正需要做的就是向用户显示一条快速消息,告知他们获得了一些数据并让他们决定要用它做什么.一旦完成,我的应用程序将继续打开并进入前台(didFinishLaunchingWithOptions冷启动中的类似代码),根据警报选择添加或不添加新数据.
我可以标记一个我检查所有viewWillAppear函数的全局变量,但由于我有30多个视图,这将是很多重复.
如果您有任何想法,请告诉我.
谢谢
格雷格
ZeM*_*oon 92
尝试使用
self.window?.rootViewController?.presentViewController(importAlert, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
您只需要一个viewController呈现AlertController 的对象.
在Swift 4中:
self.window?.rootViewController?.present(importAlert, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
Adi*_*kar 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)
})
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
| 归档时间: |
|
| 查看次数: |
33564 次 |
| 最近记录: |