UIAlertAction的编写处理程序

我向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)!)

目前,整个块/封盖都让我有些头疼。任何建议都非常感谢。

回答:

而不是将自己放在处理程序中,而应放置(提示:UIAlertAction!)。这应该使您的代码看起来像这样

    alert.addAction(UIAlertAction(title: "Okay",

style: UIAlertActionStyle.Default,

handler: {(alert: UIAlertAction!) in println("Foo")}))

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

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

以上是 UIAlertAction的编写处理程序 的全部内容, 来源链接: utcz.com/qa/399918.html

回到顶部