【问题标题】:'UIAlertView' was deprecated in iOS 9.0. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead'UIAlertView' 在 iOS 9.0 中已弃用。改用 UIAlertController 和 UIAlertControllerStyleAlert 的首选样式
【发布时间】:2016-07-23 13:01:33
【问题描述】:

我看到了更多这样的答案,但没有任何帮助。这是我较早的警报和操作

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 中已弃用。将 UIAlertController 与 改为 UIAlertControllerStyleAlert 的首选样式

我试过了:

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 新手。您的帮助将很有用。谢谢!

【问题讨论】:

  • 为什么在上述情况下需要按钮索引?在其处理程序块/闭包中编写每个按钮单击需要执行的代码。
  • @user5513630 如果我们使用 UIAlertCOntroller 而不是 UIAlertView ,那么我们可以使用这个函数“func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int){} ”如果不是那么还有什么替代方法?请帮帮我,我是新人
  • @MidhunMP 如果我们使用 UIAlertCOntroller 而不是 UIAlertView ,那么我们可以使用这个函数“func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int){} ”如果不是那么还有什么替代方法?请帮帮我,我是新人
  • @ArgaPK:你不能使用那个方法。每个按钮都有一个闭包块(请参阅 UIAlertAction),当单击该按钮时,将执行相应的闭包。检查以下答案以获得清晰的想法
  • @MidhunMP 谢谢

标签: ios swift uialertview uialertcontroller


【解决方案1】:

UIAlertController 中查看此 Code 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)

斯威夫特 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)

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

【讨论】:

  • 动作方法呢??我需要为此提及单独的方法吗??
  • No Not.This 是 AlertController 中的新功能。以及此方法中的按钮操作事件句柄。
  • 例如,当我按下确定按钮时,我需要使用这一行 UIApplication.sharedApplication().openURL(NSURL(string: "prefs:root")!) 然后我可以在代码中添加这一行
  • 而不是那个`print("ok"),我需要添加这一行...Rignt??
  • @KiritModi 如果我们使用 UIAlertCOntroller 而不是 UIAlertView ,那么我们可以使用这个函数“func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int){} ”如果不是那么还有什么替代方法?请帮帮我,我是新人
【解决方案2】:

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)

【讨论】:

    【解决方案3】:

    对于基本的警报消息,我喜欢在 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")

    【讨论】:

      【解决方案4】:

      斯威夫特 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)
      

      【讨论】:

        【解决方案5】:
          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)
        

        【讨论】:

          【解决方案6】:

          试试这个代码。 例

           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 你可以试试 Handler alert.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.Default, handler: {(alert: UIAlertAction!) in println("Your code")} ))
          【解决方案7】:

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

          How to add an action to a UIAlertView button using Swift iOS

              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)
          

          【讨论】:

            【解决方案8】:

            这段代码会有所帮助

            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 意味着我已经设置了一些动作。所以才问
            • 否,您需要在按下按钮时调用您正在打印的操作方法(例如“您已按下取消按钮”)。您需要修改此代码。
            • 只需将该打印语句替换为您的方法调用即可。简单
            • 是的,我替换了我的操作方法调用,例如 alertView 替换了该打印语句。但是没有发生任何操作。我可以调用那个动作方法吗??
            • 您正在使用旧操作的委托方法(并且该方法的参数将为 nil),因此请使用新名称创建新的自定义方法,然后尝试调用它。它肯定会工作。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2021-01-31
            • 2016-01-14
            • 1970-01-01
            • 2016-01-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多