iOS 9.0中已经弃用了'UIAlertView',请改用preferredStyle为UIAlertControllerStyleAlert的UIAlertController。

35

我看了很多答案,但都没有帮助。这是我以前的警报和相应动作

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) 
    }
}

我收到了警告:

'UIAlertView' 在iOS 9.0中已被弃用。请改用具有preferredStyle属性为UIAlertControllerStyleAlert的UIAlertController

我尝试过:

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)

但是我无法将两个按钮添加并将按钮按下方法的索引路径链接到我的旧代码,我的uialert按钮没有任何动作发生。

请帮助我解决问题,如何删除这些警告并重新编写带有我的两个按钮操作的Uialert。

我是Swift的新手。您的帮助将很有用。谢谢!


1
在上述情况下,为什么需要按钮索引?编写代码,在其处理程序块/闭包中执行每个按钮单击所需的代码。 - Midhun MP
如果我们使用UIAlertController而不是UIAlertView,那么我们能否使用这个函数 "func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int){}"?如果不能,那么有什么替代方案吗?请帮帮我,我是新手。 - ArgaPK
@MidhunMP 如果我们使用UIAlertController而不是UIAlertView,那么我们能否使用这个函数“func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int){}”?如果不能,那么有什么替代方案吗?请帮帮我,我是新手。 - ArgaPK
1
@ArgaPK:你不能使用那个方法。对于每个按钮,都有一个闭包块(参见UIAlertAction),当点击该按钮时,相应的闭包将被执行。请查看以下答案以获得清晰的理解。 - Midhun MP
@MidhunMP 谢谢您 - ArgaPK
8个回答

73

查看在 UIAlertController 中的“Destructive”和“OK”按钮代码:

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)

查看带有破坏性和确定按钮的警报:

输入图像描述


@KiritModi 如果我们使用UIAlertController而不是UIAlertView,那么我们能否使用这个函数 "func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int){}"?如果不能,那么有什么替代方案吗?请帮帮我,我是新手。 - ArgaPK
在UIAlertController中,有单独的按钮方法。因此,在此按钮中编写代码。您可以看到上面代码中的“确定”按钮。 - Kirit Modi
@KiritModi 你的意思是没有像func alertView(alertView: UIAlertView, clickedButtonAtIndex bi: Int){}这样的东西吗?如果是的话,我们该如何编写方法?使用UIAertAction吗?我们可以在按钮OK的UIALertAction中编写这个“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) }”吗? - ArgaPK
@KiritModi 请问 " UIApplication.sharedApplication().openURL(NSURL(string: "prefs:root")!) " 这段代码是什么意思?这行代码会发生什么事情? - ArgaPK
让我们在聊天中继续这个讨论 - ArgaPK
显示剩余7条评论

15

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)

11

对于基本的警报信息,我喜欢在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: "测试标题", message: "测试消息")


2

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)

1
  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)

0
尝试这段代码。

Ex

 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)

如果我们使用UIAlertController而不是UIAlertView,那么我们能否使用这个函数“func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int){}”?如果不能,那么有什么替代方案吗?请帮帮我,我是新手。 - ArgaPK
@ArgaPK 你可以尝试使用Handler alert.addAction(UIAlertAction(title: "确定", style: UIAlertActionStyle.Default, handler: {(alert: UIAlertAction!) in println("你的代码")})) - Avijit Nagare

0

这段代码将会有所帮助

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)

因为我将按钮点击设置为0,1,这意味着我在那里设置了一些操作。所以只是询问。 - user5513630
不,当按钮被按下时,您需要调用您的操作方法,在那里我正在打印(例如“您已按下取消按钮”)。您需要修改此代码。 - Govind Prajapati
只需将那个打印语句替换为您的方法调用即可。简单易行。 - Govind Prajapati
是的,我用打印语句替换了我的动作方法调用,比如alertView。但是没有任何动作发生。我调用那个动作方法是正确的吗? - user5513630
你正在使用旧的操作委托方法(并且该方法的参数将为nil),因此请创建一个新的自定义方法,并使用新名称调用它。这肯定会起作用。 - Govind Prajapati

0

我从这个链接中得到了答案,我认为它对你很有用

如何使用Swift iOS向UIAlertView按钮添加操作

    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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接