如何从UIAlertView迁移(在iOS8中不推荐)

我目前在我的一个应用程序中有以下代码行。 这是一个简单的UIAlertView 。 不过,从iOS 8开始,现在已经弃用了:

 let alertView = UIAlertView(title: "Oops!", message: "This feature isn't available right now", delegate: self, cancelButtonTitle: "OK") 

我如何更新这个工作与iOS 8 +? 我相信我必须改变一些UIAlertCotroller ,虽然我不太清楚什么。

您需要改用UIAlertController 。 类文档非常简单,甚至在文档的开头部分包含清单1中的用法示例(确定它在ObjC中,而不是在Swift中,但非常相似)。

所以对于你的用例,这里是如何转化为(与添加评论):

 let alert = UIAlertController(title: "Oops!", message:"This feature isn't available right now", preferredStyle: .alert) let action = UIAlertAction(title: "OK", style: .default) { _ in // Put here any code that you would like to execute when // the user taps that OK button (may be empty in your case if that's just // an informative alert) } alert.addAction(action) self.presentViewController(alert, animated: true){} 

所以紧凑的代码将如下所示:

 let alert = UIAlertController(title: "Oops!", message:"This feature isn't available right now", preferredStyle: .Alert) alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in }) self.present(alert, animated: true){} 

在这里self应该是你的UIViewController


附加提示:如果您需要调用在UIViewController的上下文之外显示警报的代码(其中self不是UIViewController ),则始终可以使用应用程序的根VC:

 let rootVC = UIApplication.sharedApplication().keyWindow?.rootViewController rootVC?.presentViewController(alert, animated: true){} 

(但总的来说,当你有一个UIViewController时候,最好使用一个已知的UIViewController – 而且你通常会提示来自UIViewControllers的警报 – 或者根据你的上下文而不是依赖这个提示来获得最合适的警告)

对于那些想知道如何在Objective-C中做到这一点的人:

  //Step 1: Create a UIAlertController UIAlertController *myAlertController = [UIAlertController alertControllerWithTitle:@"MyTitle" message: @"MyMessage" preferredStyle:UIAlertControllerStyleAlert ]; //Step 2: Create a UIAlertAction that can be added to the alert UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { //Do some thing here, eg dismiss the alertwindow [myAlertController dismissViewControllerAnimated:YES completion:nil]; }]; //Step 3: Add the UIAlertAction ok that we just created to our AlertController [myAlertController addAction: ok]; //Step 4: Present the alert to the user [self presentViewController:myAlertController animated:YES completion:nil]; 

这会popup一个如下所示的警报:

警示例子

 let alertView = UIAlertView(title: "Oops!", message: "This feature isn't available right now", delegate: self, cancelButtonTitle: "OK") 

 let alertController = UIAlertController(title: "Oops!", message: "This feature isn't available right now", preferredStyle: .Alert) let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in } alertController.addAction(OKAction) self.presentViewController(alertController, animated: true) { } 

我认为这是对旧的iOS SDK具有向后兼容性的方式,并且在使用较新的SDK时使用新的API。 此外,不使用已弃用类的代码中的弃用警告。

  if ([UIAlertController class]) { // Use new API to create alert controller, add action button and display it UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"CityBoard" message:error.errorDescription preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* ok = [UIAlertAction actionWithTitle: @"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { [alertController dismissViewControllerAnimated:YES completion:nil]; }]; [alertController addAction: ok]; [self presentViewController:alertController animated:YES completion:nil]; } else { // We are running on old SDK as the new class is not available // Hide the compiler errors about deprecation and use the class available on older SDK #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"CityBoard" message:error.errorDescription delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; #pragma clang diagnostic pop 

Swift 2.0:

使用AlertController。

操作表示例:

  let mediaActionSheet: UIAlertController = UIAlertController(title: "Media Action Sheet", message: "Choose an option!", preferredStyle: .ActionSheet) //Create and add the Cancel action let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in //Just dismiss the action sheet } mediaActionSheet.addAction(cancelAction) //Create and add first option action let takePictureAction: UIAlertAction = UIAlertAction(title: "Take Picture", style: .Default) { action -> Void in //Code for launching the camera goes here } mediaActionSheet.addAction(takePictureAction) //Create and add a second option action let choosePictureAction: UIAlertAction = UIAlertAction(title: "Choose From Gallery", style: .Default) { action -> Void in //Code for picking from gallery goes here } mediaActionSheet.addAction(choosePictureAction) //Present the AlertController self.presentViewController(mediaActionSheet, animated: true, completion: nil) 

警报示例:

1)

 let simpleAlert = UIAlertController(title: "Simple Alert", message: "It is just awesome", preferredStyle: UIAlertControllerStyle.Alert); //show it showViewController(simpleAlert, sender: self); 

2)警报与TextField中。

  let inputTextFieldAlert:UIAlertController = UIAlertController(title: " Input TextField Alert ", message: " Enter on the below TextField ", preferredStyle: UIAlertControllerStyle.Alert); //default input textField (no configuration...) inputTextFieldAlert.addTextFieldWithConfigurationHandler(nil); //no event handler (just close dialog box) inputTextFieldAlert.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.Cancel, handler: nil)); //event handler with closure inputTextFieldAlert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default, handler: {(action:UIAlertAction) in let fields = inputTextFieldAlert.textFields!; print("Output: "+fields[0].text!); })); presentViewController(inputTextFieldAlert, animated: true, completion: nil); 

3)

 var alert = UIAlertController(title: "TextField Alert", message: "Enter on the below TextField", preferredStyle: UIAlertControllerStyle.Alert); //configured input textField var field:UITextField?; alert.addTextFieldWithConfigurationHandler({(input:UITextField)in input.placeholder="Empty Dtaa ;-)"; input.clearButtonMode=UITextFieldViewMode.WhileEditing; field=input; }); //YES Handler func yesHandler(actionTarget: UIAlertAction){ print(field!.text!); } //event handler with predefined function alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default, handler: yesHandler)); presentViewController(alert, animated: true, completion: nil); 

上面的例子并没有太大的帮助。 我的解决scheme是用于Xcode 6.4,Swift 1.2,你可以复制和粘贴这个代码在一个testing项目,以感受它是如何工作的:

解决scheme1 ​​ – Swift 1.2:

 import UIKit let ALERT_TITLE = "Got you working, right?" let ALERT_MESSAGE = "Well maybe..." class ViewController: UIViewController { private var alert: UIAlertController! private var presentAlertButton: UIButton! override func viewDidAppear(animated: Bool) { /* // QUICK TEST - 1 self.presentViewController(alert, animated: true, completion: nil) */ // QUCIK TEST - 2 /* let rootVC = UIApplication.sharedApplication().keyWindow?.rootViewController rootVC?.presentViewController(alert, animated: true, completion: nil) */ } override func viewDidLoad() { super.viewDidLoad() createAndAddAlertV() createAndAddAlertButton() } private func createAndAddAlertV() { alert = UIAlertController(title:ALERT_TITLE, message:ALERT_MESSAGE, preferredStyle: .Alert) let alertAction = UIAlertAction(title: "OK", style: .Default, handler: nil) alert.addAction(alertAction) } private func createAndAddAlertButton() { presentAlertButton = UIButton(frame: CGRectMake( view.frame.size.width / 2, view.frame.size.height / 2, 200, 100)) presentAlertButton.layer.anchorPoint = CGPointMake(1.0, 1.0) presentAlertButton.backgroundColor = UIColor.redColor() presentAlertButton.setTitle("Click For Alert", forState: .Normal) presentAlertButton.addTarget(self, action: "showAlertV", forControlEvents: .TouchUpInside) self.view.addSubview(presentAlertButton) } @IBAction func showAlertV() { println(" Showing... ") self.presentViewController(alert, animated: true, completion: nil) } } 

我在Xcode 7.0中检查了这个解决scheme。 有效。 Xcode做了一个改变。 我再次在Xcode 6.4重新编译它,它工作。 Swift 2.0的变化应该是微小的,如果根本不存在的话。

希望这可以帮助 ;)

您可以将此代码用于警报视图:

  UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]; [alertController addAction:ok]; [self presentViewController:alertController animated:YES completion:nil]; 

对于多个button,您可以使用:

  UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert]; [alertController addAction:[UIAlertAction actionWithTitle:@"Button 1" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [self loadGooglrDrive]; }]]; [alertController addAction:[UIAlertAction actionWithTitle:@"Button 2" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [self loadDropBox]; }]]; [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [self closeAlertview]; }]]; dispatch_async(dispatch_get_main_queue(), ^ { [self presentViewController:alertController animated:YES completion:nil]; }); -(void)closeAlertview { [self dismissViewControllerAnimated:YES completion:nil]; }