我正在向用户展示一个 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)!)
目前对闭包还不是很了解,有什么建议将不胜感激。
{alert in println("Foo")}
,{_ in println("Foo")}
, 和{println("Foo")}
应该也可以工作。 (注:这是Swift中的一些闭包表达式,它们都可以用来执行打印 "Foo" 的操作。第一个闭包使用了参数名 alert,但没有使用该参数;第二个闭包使用下划线 _ 表示未使用的参数;第三个闭包没有任何参数。) - Brian Nickelalert.addAction(UIAlertAction(title: "确定", style: .default) { _ in println("Foo") })
- Elijah