2015-08-27 38 views
-1

我正在嘗試添加一個操作表。從我讀過的內容來看,UIActionSheet在IOS 8中折舊,而應該使用Alert Controller。這裏是我的代碼片段:Swift - UIAlertAction - 找不到成員Default?

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有三種可能的樣式:默認,取消&破壞性。我究竟做錯了什麼?

非常感謝:)

回答

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() 
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 

     })