为UIAlertAction编写处理程序

时间:2014-06-12 17:17:30

标签: ios uialertview swift uialertcontroller

我向用户展示了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)!)

目前整个街区/封闭区有点过头了。任何建议都非常感谢。

9 个答案:

答案 0 :(得分:143)

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

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

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

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

答案 1 :(得分:12)

你可以使用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")
    }

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

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

答案 2 :(得分:10)

让我们假设你想要一个带有主标题的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 Version 7.0 beta 3)

答案 3 :(得分:5)

swift 3.0中的语法更改

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

答案 4 :(得分:4)

这是我用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")
    }

答案 5 :(得分:2)

创建警报,在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!)
{
}

答案 6 :(得分:2)

swift4中的

:  let alert = UIAlertController(title:“someAlert”,message:“someMessage”,preferredStyle:UIAlertControllerStyle.alert)

    alert.addAction(UIAlertAction(title: "ok", style: UIAlertActionStyle.default, handler: {
        _ in print("FOO ")
    }))

现在(警报,动画:真实,完成:无)

答案 7 :(得分:1)

  1. 在Swift中

    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];
    

答案 8 :(得分:0)

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

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

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

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