【问题标题】:Writing handler for UIAlertAction为 UIAlertAction 编写处理程序
【发布时间】:2014-08-03 03:00:21
【问题描述】:

我正在向用户展示UIAlertView,但我不知道如何编写处理程序。这是我的尝试:

let alert = UIAlertController(title: "Title",
                            message: "Message",
                     preferredStyle: UIAlertControllerStyle.Alert)

alert.addAction(UIAlertAction(title: "Okay",
                              style: UIAlertActionStyle.Default,
                            handler: {self in println("Foo")})

我在 Xcode 中遇到了一堆问题。

文档说convenience init(title title: String!, style style: UIAlertActionStyle, handler handler: ((UIAlertAction!) -> Void)!)

目前,整个街区/封闭区有点超出我的想象。任何建议都非常感谢。

【问题讨论】:

    标签: ios uialertview swift uialertcontroller


    【解决方案1】:

    在你的处理程序中,而不是 self ,放入 (alert: UIAlertAction!)。这应该使您的代码看起来像这样

        alert.addAction(UIAlertAction(title: "Okay",
                              style: UIAlertActionStyle.Default,
                            handler: {(alert: UIAlertAction!) in println("Foo")}))
    

    这是在 Swift 中定义处理程序的正确方法。

    正如 Brian 在下面指出的,还有更简单的方法来定义这些处理程序。书中讨论了使用他的方法,请查看标题为闭包的部分

    【讨论】:

    • {alert in println("Foo")}{_ in println("Foo")}{println("Foo")} 也应该可以工作。
    • @BrianNickel:第三个不起作用,因为您需要处理参数操作。但除此之外,您不必快速编写 UIAlertActionStyle.Default 。 .Default 也可以。
    • 请注意,如果您使用“let foo = UIAlertAction(...)”,那么您可以使用尾随闭包语法在 UIAlertAction 之后放置可能很长的闭包 - 它看起来很不错方式。
    • 这是一个优雅的写法:alert.addAction(UIAlertAction(title: "Okay", style: .default) { _ in println("Foo") })
    【解决方案2】:

    函数是 Swift 中的一等对象。因此,如果您不想使用闭包,也可以只定义一个具有适当签名的函数,然后将其作为handler 参数传递。观察:

    func someHandler(alert: UIAlertAction!) {
        // Do something...
    }
    
    alert.addAction(UIAlertAction(title: "Okay",
                                  style: UIAlertActionStyle.Default,
                                  handler: someHandler))
    

    【讨论】:

    【解决方案3】:

    假设您想要一个 UIAlertAction 主标题、两个操作(保存和丢弃)和取消按钮:

    let actionSheetController = UIAlertController (title: "My Action Title", message: "", preferredStyle: UIAlertControllerStyle.ActionSheet)
    
        //Add Cancel-Action
        actionSheetController.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
    
        //Add Save-Action
        actionSheetController.addAction(UIAlertAction(title: "Save", style: UIAlertActionStyle.Default, handler: { (actionSheetController) -> Void in
            print("handle Save action...")
        }))
    
        //Add Discard-Action
        actionSheetController.addAction(UIAlertAction(title: "Discard", style: UIAlertActionStyle.Default, handler: { (actionSheetController) -> Void in
            print("handle Discard action ...")
        }))
    
        //present actionSheetController
        presentViewController(actionSheetController, animated: true, completion: nil)
    

    这适用于 swift 2(Xcode 版本 7.0 beta 3)

    【讨论】:

      【解决方案4】:

      您可以使用 swift 2 做到这一点:

      let alertController = UIAlertController(title: "iOScreator", message:
              "Hello, world!", preferredStyle: UIAlertControllerStyle.Alert)
      alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Destructive,handler: { action in
              self.pressed()
      }))
      
      func pressed()
      {
          print("you pressed")
      }
      
          **or**
      
      
      let alertController = UIAlertController(title: "iOScreator", message:
              "Hello, world!", preferredStyle: UIAlertControllerStyle.Alert)
      alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Destructive,handler: { action in
            print("pressed")
       }))
      

      以上所有答案都是正确的,我只是展示了另一种可以完成的方法。

      【讨论】:

        【解决方案5】:

        这就是我使用 xcode 7.3.1 的方式

        // create function
        func sayhi(){
          print("hello")
        }
        

        //创建按钮

        let sayinghi = UIAlertAction(title: "More", style: UIAlertActionStyle.Default, handler:  { action in
                    self.sayhi()})
        

        // 将按钮添加到警报控件

        myAlert.addAction(sayhi);
        

        //整个代码,这段代码会添加2个按钮

          @IBAction func sayhi(sender: AnyObject) {
                let myAlert = UIAlertController(title: "Alert", message:"sup", preferredStyle: UIAlertControllerStyle.Alert);
                let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler:nil)
        
                let sayhi = UIAlertAction(title: "say hi", style: UIAlertActionStyle.Default, handler:  { action in
                    self.sayhi()})
        
                // this action can add to more button
                myAlert.addAction(okAction);
                myAlert.addAction(sayhi);
        
                self.presentViewController(myAlert, animated: true, completion: nil)
            }
        
            func sayhi(){
                // move to tabbarcontroller
             print("hello")
            }
        

        【讨论】:

          【解决方案6】:

          swift 3.0 中的语法变化

          alert.addAction(UIAlertAction(title: "Okay",
                          style: .default,
                          handler: { _ in print("Foo") } ))
          

          【讨论】:

            【解决方案7】:

            创建警报,在 xcode 9 中测试

            let alert = UIAlertController(title: "title", message: "message", preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: self.finishAlert))
            self.present(alert, animated: true, completion: nil)
            

            和功能

            func finishAlert(alert: UIAlertAction!)
            {
            }
            

            【讨论】:

              【解决方案8】:

              在 Swift 4 中:

              let alert=UIAlertController(title:"someAlert", message: "someMessage", preferredStyle:UIAlertControllerStyle.alert )
              
              alert.addAction(UIAlertAction(title: "ok", style: UIAlertActionStyle.default, handler: {
                      _ in print("FOO ")
              }))
              
              present(alert, animated: true, completion: nil)
              

              【讨论】:

                【解决方案9】:
                1. 在斯威夫特中

                  let alertController = UIAlertController(title:"Title", message: "Message", preferredStyle:.alert)
                  
                  let Action = UIAlertAction.init(title: "Ok", style: .default) { (UIAlertAction) in
                      // Write Your code Here
                  }
                  
                  alertController.addAction(Action)
                  self.present(alertController, animated: true, completion: nil)
                  
                2. 在目标 C 中

                  UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
                  
                  UIAlertAction *OK = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
                  {
                  }];
                  
                  [alertController addAction:OK];
                  
                  [self presentViewController:alertController animated:YES completion:nil];
                  

                【讨论】:

                  【解决方案10】:

                  在 Swift 5 中,您还可以执行以下操作:

                  let handler = { (action: UIAlertAction!) -> Void in
                      //do tasks
                  }
                  

                  然后,当您进行操作时,只需将其替换为:

                  let action = UIAlertAction(title: "Do it", style: .default, handler: handler)
                  

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2019-07-12
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多