Swift - UIAlertAction - 找不到名为Default的成员?

时间:2015-08-27 18:25:22

标签: xcode swift uiactionsheet

我正在尝试添加操作表。根据我的阅读,UIActionSheet在IOS 8中被折旧,而且应该使用警报控制器。这是我的代码片段:

let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
let takePhoto = UIAlertAction(title: "Take Photo", style: .Default, handler: { (alert: UIAlertAction) -> Void in
//action goes here 
})

然而。我收到一条错误,上面写着“找不到会员'默认'”。根据我的理解,UIAlertAction有3种可能的样式:默认,取消&破坏性。我做错了什么?

非常感谢:)

2 个答案:

答案 0 :(得分:0)

问题实际上是关闭,而不是.Default,但这就是编译器所抱怨的。警报类型不正确(只是勉强)。您可以将闭包定义更改为其中一个以使其起作用:

... handler: { (alert: UIAlertAction!) -> Void in
... handler: { alert -> Void in
... handler: { _ -> Void in //Use this if you don't care about the alert object

无耻插件:您还可以使用类似LKAlertController的内容来轻松创建操作表。它会变成类似

的东西
ActionSheet().addAction(title: "Take Photo", style: .Default) {
    //Action
}.addAction(/* Your other actions */).show()

答案 1 :(得分:0)

这可以编译,但是您还可以添加.addAction等等。请注意,optionMenu中的首选样式是.Alert。 Vandad Nahavandipoor在IOS 8 Swift Cookbook中有一个很好的解释

        let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .Alert)
        let takePhoto = UIAlertAction(title: "Take Photo",
        style: UIAlertActionStyle.Default,
        handler: {[weak self] (paramAction:UIAlertAction!) in

        })