为UIAlertAction编写处理程序

Ste*_*all 92 uialertview ios 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")})
Run Code Online (Sandbox Code Playgroud)

我在Xcode中遇到了很多问题.

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

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

jbm*_*223 153

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

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

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

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

  • `{alert in println("Foo")}`,`{_ in println("Foo")}`和`{println("Foo")}`也应该有效. (9认同)
  • @BrianNickel:第三个不起作用,因为你需要处理参数动作.但除此之外,您不必在swift中编写UIAlertActionStyle.Default..Default也有效. (7认同)

Rob*_*sen 68

函数是Swift中的第一类对象.因此,如果您不想使用闭包,您还可以使用适当的签名定义一个函数,然后将其作为handler参数传递.注意:

func someHandler(alert: UIAlertAction!) {
    // Do something...
}

alert.addAction(UIAlertAction(title: "Okay",
                              style: UIAlertActionStyle.Default,
                              handler: someHandler))
Run Code Online (Sandbox Code Playgroud)


Kor*_*pel 14

你可以使用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")
 }))
Run Code Online (Sandbox Code Playgroud)

要么

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")
 }))
Run Code Online (Sandbox Code Playgroud)

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


pol*_*war 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)
Run Code Online (Sandbox Code Playgroud)

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


小智 9

在 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)
Run Code Online (Sandbox Code Playgroud)


Fan*_*ing 5

Swift 3.0中的语法更改

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