为UIAlertAction编写处理程序 [英] Writing handler for UIAlertAction
问题描述
我正在向用户展示 UIAlertView
,我无法弄清楚如何编写处理程序。这是我的尝试:
I'm presenting a UIAlertView
to the user and I can't figure out how to write the handler. This is my attempt:
let alert = UIAlertController(title: "Title",
message: "Message",
preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Okay",
style: UIAlertActionStyle.Default,
handler: {self in println("Foo")})
我在Xcode中遇到了很多问题。
I get a bunch of issues in Xcode.
文档说便利init(标题:String!,样式:UIAlertActionStyle,处理程序处理程序:((UIAlertAction!) - > Void)!)
目前整个区块/关闭都有点过头了。任何建议都非常感谢。
The whole blocks/closures is a little over my head at the moment. Any suggestion are much appreciated.
推荐答案
而不是自己在你的处理程序中,put(alert:UIAlertAction!)。这应该使你的代码看起来像这样
Instead of self in your handler, put (alert: UIAlertAction!). This should make your code look like this
alert.addAction(UIAlertAction(title: "Okay",
style: UIAlertActionStyle.Default,
handler: {(alert: UIAlertAction!) in println("Foo")}))
这是在Swift中定义处理程序的正确方法。
this is the proper way to define handlers in Swift.
正如Brian指出的那样,有还有更简单的方法来定义这些处理程序。书中讨论了使用他的方法,请看标题为Closures
As Brian pointed out below, there are also easier ways to define these handlers. Using his methods is discussed in the book, look at the section titled Closures
这篇关于为UIAlertAction编写处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!