在iOS 9.0中,“UIAlertView”已被弃用。 相反,使用UIAlertController和UIAlertControllerStyleAlert的preferredStyle

我已经看到了更多的答案,但没有什么帮助。这是我的老警戒和行动

override func viewWillAppear(animated: Bool) { if Reachability.isConnectedToNetwork() == true { print("internet connection ok") } else { print("internet not ok") let alertView: UIAlertView = UIAlertView(title: "Alert ", message: "connect to internet", delegate: self, cancelButtonTitle: "settings", otherButtonTitles: "cancel") alertView.show() return } } func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) { if buttonIndex == 0 { //This will open ios devices wifi settings UIApplication.sharedApplication().openURL(NSURL(string: "prefs:root")!) } else if buttonIndex == 1 { //TODO for cancel exit(0) } } 

在这我得到警告:

在iOS 9.0中,“UIAlertView”已被弃用。 相反,使用UIAlertController和UIAlertControllerStyleAlert的preferredStyle

我试过了 :

 let alert = UIAlertController(title: "Alert", message: "My Alert for test", preferredStyle: UIAlertControllerStyle.Alert) alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)) alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: { (action:UIAlertAction!) in print("you have pressed the Cancel button") })) self.presentViewController(alert, animated: true, completion: nil) 

但要添加两个button,并添加button按下方法的索引path链接我的旧代码,我无法做到这一点。 没有什么行动发生在我的uialertbutton,

请帮助我,我怎样才能删除警告,并重新编码我的Uialert与我的两个button的行动。

我是新来的,你的帮助将是有益的。谢谢!

引用UIAlertController

请参阅UIAlertController代码破坏性和确定button:

 let alertController = UIAlertController(title: "Destructive", message: "Simple alertView demo with Destructive and Ok.", preferredStyle: UIAlertControllerStyle.alert) //Replace UIAlertControllerStyle.Alert by UIAlertControllerStyle.alert let DestructiveAction = UIAlertAction(title: "Destructive", style: UIAlertActionStyle.Destructive) { (result : UIAlertAction) -> Void in print("Destructive") } // Replace UIAlertActionStyle.Default by UIAlertActionStyle.default let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in print("OK") } alertController.addAction(DestructiveAction) alertController.addAction(okAction) self.presentViewController(alertController, animated: true, completion: nil) 

Swift 3:

 let alertController = UIAlertController(title: "Destructive", message: "Simple alertView demo with Destructive and Ok.", preferredStyle: UIAlertControllerStyle.alert) //Replace UIAlertControllerStyle.Alert by UIAlertControllerStyle.alert let DestructiveAction = UIAlertAction(title: "Destructive", style: UIAlertActionStyle.destructive) { (result : UIAlertAction) -> Void in print("Destructive") } // Replace UIAlertActionStyle.Default by UIAlertActionStyle.default let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in print("OK") } alertController.addAction(DestructiveAction) alertController.addAction(okAction) self.present(alertController, animated: true, completion: nil) 

见具有破坏性和确定button的警报:

在这里输入图像说明

Swift 3中 ,你可以这样写:

 let alertController = UIAlertController(title: "Title", message: "This is my text", preferredStyle: UIAlertControllerStyle.alert) let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in print("You pressed OK") } alertController.addAction(okAction) self.present(alertController, animated: true, completion: nil) 

对于我喜欢在UIViewController上使用扩展的基本警报消息:

 extension UIViewController { func alertMessageOk(title: String, message: String) { let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert) let action = UIAlertAction(title: "Ok", style: .default, handler: nil) alert.addAction(action) present(alert, animated: true, completion: nil) } } 

用法: self.alertMessageOk(title: "Test Title", message: "Test message")

  UIAlertController *AC = UIAlertController.alertControllerWithTitle("Title",message:"Message",preferredStyle:UIAlertControllerStyleAlert) UIAlertAction *ActionOne = [UIAlertAction actionWithTitle:"ActionOne" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog("ActionOne") } ] UIAlertAction *ActionTwo = [UIAlertAction actionWithTitle:"ActionTwo" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog("ActionTwo") } ] AC.addAction(ActionOne) AC.addAction(ActionTwo) self.presentViewController(AC,animated:true,completion:nil) 

Swift 3

  // Create message let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .actionSheet) // Clear Action let clearAction = UIAlertAction(title: "Clear", style: .destructive, handler: { (action:UIAlertAction!) in print ("clear") }) alertController.addAction(clearAction) // Cancel let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: { (action:UIAlertAction!) in print ("Cancel") }) alertController.addAction(cancelAction) // Present Alert self.present(alertController, animated: true, completion:nil) 

试试这个代码。 防爆

  let actionSheetController: UIAlertController = UIAlertController(title: "Are you sure?", message: "", preferredStyle: .Alert) let cancelAction: UIAlertAction = UIAlertAction(title: "NO", style: .Cancel) { action -> Void in //Do your task } actionSheetController.addAction(cancelAction) let nextAction: UIAlertAction = UIAlertAction(title: "YES", style: .Default) { action -> Void in //Do your task //NSUserDefaults.standardUserDefaults().removePersistentDomainForName(NSBundle.mainBundle().bundleIdentifier!) // NSUserDefaults.standardUserDefaults().synchronize() } actionSheetController.addAction(nextAction) self.presentViewController(actionSheetController, animated: true, completion: nil) 

我已经从这个链接得到这个答案,我认为这对你有用

如何使用Swift iOS将操作添加到UIAlertViewbutton

  var alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert) // Create the actions var okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { UIAlertAction in NSLog("OK Pressed") } var cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { UIAlertAction in NSLog("Cancel Pressed") } // Add the actions alertController.addAction(okAction) alertController.addAction(cancelAction) // Present the controller self.presentViewController(alertController, animated: true, completion: nil) 

这段代码会有所帮助

 let alertController = UIAlertController(title: "Default AlertController", message: "A standard alert", preferredStyle: .Alert) let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action:UIAlertAction!) in print("you have pressed the Cancel button"); } alertController.addAction(cancelAction) let OKAction = UIAlertAction(title: "OK", style: .Default) { (action:UIAlertAction!) in print("you have pressed OK button"); } alertController.addAction(OKAction) self.presentViewController(alertController, animated: true, completion:nil)