【问题标题】:How to add an action to a UIAlertView button using Swift iOS如何使用 Swift iOS 向 UIAlertView 按钮添加操作
【发布时间】:2014-08-03 10:08:09
【问题描述】:

我想添加另一个按钮,而不是“确定”按钮,它应该只是关闭警报。 我想让另一个按钮调用某个函数。

var logInErrorAlert: UIAlertView = UIAlertView()
logInErrorAlert.title = "Ooops"
logInErrorAlert.message = "Unable to log in."
logInErrorAlert.addButtonWithTitle("Ok")

如何向此警报添加另一个按钮,然后允许它在单击后调用一个函数,因此假设我们希望调用新按钮:

 retry()

【问题讨论】:

    标签: ios uialertview swift


    【解决方案1】:

    Swifty 的方式是使用新的 UIAlertController 和闭包:

        // Create the alert controller
        let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert)
    
        // Create the actions
        let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
            UIAlertAction in
            NSLog("OK Pressed")
        }
        let 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)
    

    斯威夫特 3:

        // Create the alert controller
        let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
    
        // Create the actions
        let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
            UIAlertAction in
            NSLog("OK Pressed")
        }
        let 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.present(alertController, animated: true, completion: nil)
    

    【讨论】:

    • 不幸的是,我目前正在使用 iOS 7.1,而 UIAlertController 仅在 iOS 8.0 + 中可用。所以我必须使用 UIAlertView
    • 我做的和你一模一样,但错误没有出现,而是在日志中显示“警告,尝试在 <_ttc13nsalert_swift14viewcontroller:> 上显示 ,其视图不在窗口层次结构!”知道是什么问题吗?
    • 在我的情况下,出现警报但按钮没有响应触摸。也就是说,“OK Pressed”和“Cancel Pressed”都没有出现。
    【解决方案2】:
    func showAlertAction(title: String, message: String){
        let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
        alert.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: {(action:UIAlertAction!) in
            print("Action")
        }))
        alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }
    

    【讨论】:

      【解决方案3】:

      UIAlertViews 使用代理与您(客户)进行通信。

      您添加第二个按钮,并创建一个对象来接收来自视图的委托消息:

      class LogInErrorDelegate : UIAlertViewDelegate {
      
          init {}
      
          // not sure of the prototype of this, you should look it up
          func alertView(view :UIAlertView, clickedButtonAtIndex :Integer) -> Void {
              switch clickedButtonAtIndex {
                  case 0: 
                     userClickedOK() // er something
                  case 1:
                     userClickedRetry()
                    /* Don't use "retry" as a function name, it's a reserved word */
      
                  default:
                     userClickedRetry()
              }
          }
      
          /* implement rest of the delegate */
      }
      logInErrorAlert.addButtonWithTitle("Retry")
      
      var myErrorDelegate = LogInErrorDelegate()
      logInErrorAlert.delegate = myErrorDelegate
      

      【讨论】:

        【解决方案4】:

        Swift 4 更新

                // Create the alert controller
                let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
        
                // Create the actions
                let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
                    UIAlertAction in
                    NSLog("OK Pressed")
                }
                let 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.present(alertController, animated: true, completion: nil)
        

        【讨论】:

        • UIAlertActionStyle.default 重命名为 UIAlertAction.Style.default
        【解决方案5】:

        基于 swift:

        let alertCtr = UIAlertController(title:"Title", message:"Message", preferredStyle: .Alert)
        let Cancel = AlertAction(title:"remove", style: .Default, handler: {(UIAlertAction) -> Void in })
        let Remove = UIAlertAction(title:"remove", style: .Destructive, handler:{(UIAlertAction)-> Void 
            inself.colorLabel.hidden = true    
        })
        alertCtr.addAction(Cancel)
        alertCtr.addAction(Remove)
        
        self.presentViewController(alertCtr, animated:true, completion:nil)}
        

        【讨论】:

          【解决方案6】:

          Jake 答案的 Swift 3.0 版本

          // 创建警报控制器

          let alertController = UIAlertController(title: "Alert!", message: "There is no items for the current user", preferredStyle: .alert)
          
                      // Create the actions
                      let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
                          UIAlertAction in
                          NSLog("OK Pressed")
                      }
                      let 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.present(alertController, animated: true, completion: nil)
          

          【讨论】:

            【解决方案7】:

            斯威夫特 5+

            let alert = UIAlertController(title: "Cart!", message: "Are you sure want to remove order details?", preferredStyle: .alert)
                    // Create the actions
            let okAction = UIAlertAction(title: "YES", style: 
                UIAlertAction.Style.default) {
                   UIAlertAction in
                   print("Yes Pressed")
            }
            let cancelAction = UIAlertAction(title: "CANCEL", style: 
                UIAlertAction.Style.cancel) {
                   UIAlertAction in
                   print("Cancel Pressed")
                }
            // Add the actions
            alert.addAction(okAction)
            alert.addAction(cancelAction)
            
            self.present(alert, animated: true, completion: nil)
            

            let alert = UIAlertController(title: "Alert!", message: "Are you sure want to remove your request?", preferredStyle: .alert)
                            // Create the actions
                    let okAction = UIAlertAction(title: "YES", style: .destructive) {
                           _ in
                           print("Yes Pressed")
                        self.funRemoveRequest(requestID: (self.requestStatusModel?.data?[sender.tag].id)!)
                    }
                    let cancelAction = UIAlertAction(title: "CANCEL", style: .cancel) {
                           _ in
                           print("Cancel Pressed")
                        }
                    // Add the actions
                    alert.addAction(okAction)
                    alert.addAction(cancelAction)
            
                    self.present(alert, animated: true, completion: nil)
            

            【讨论】:

              【解决方案8】:

              查看我的代码:

               @IBAction func foundclicked(sender: AnyObject) {
              
                          if (amountTF.text.isEmpty)
                          {
                              let alert = UIAlertView(title: "Oops! Empty Field", message: "Please enter the amount", delegate: nil, cancelButtonTitle: "OK")
              
                              alert.show()
                          }
              
                          else {
              
                          var alertController = UIAlertController(title: "Confirm Bid Amount", message: "Final Bid Amount : "+amountTF.text , preferredStyle: .Alert)
                          var okAction = UIAlertAction(title: "Confirm", style: UIAlertActionStyle.Default) {
                              UIAlertAction in
              
                              JHProgressHUD.sharedHUD.loaderColor = UIColor.redColor()
                                      JHProgressHUD.sharedHUD.showInView(self.view, withHeader: "Amount registering" , andFooter: "Loading")
                              }
                          var cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) {
                              UIAlertAction in
              
                              alertController .removeFromParentViewController()
                          }
                                          alertController.addAction(okAction)
                                          alertController.addAction(cancelAction)
              
                              self.presentViewController(alertController, animated: true, completion: nil)
              
                          }
              
                      }
              

              【讨论】:

                【解决方案9】:

                这适用于 swift 4.2、5 和 5+

                let alert = UIAlertController(title: "ooops!", message: "Unable to login", preferredStyle: .alert)
                
                alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
                
                self.present(alert, animated: true)
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2022-07-07
                  • 1970-01-01
                  • 2018-02-18
                  相关资源
                  最近更新 更多