1
我試圖將動作/事件添加到我的動作表中,但是當按下按鈕時,什麼也沒有發生。 我的代碼如下所示:Swift - UIActionSheet事件不起作用
@IBAction func sendTapped() {
var sheet: UIActionSheet = UIActionSheet()
let title: String = "Choose item to send:"
sheet.title = title
sheet.addButtonWithTitle("Cancel")
sheet.addButtonWithTitle("Text")
sheet.addButtonWithTitle("Image")
sheet.cancelButtonIndex = 0
sheet.delegate = self
sheet.tag = 1
sheet.showInView(self.view)
func actionSheet(sheet: UIActionSheet!, clickedButtonAtIndex buttonIndex: Int) {
switch (sheet.tag){
case 1:
println("1 pressed")
case 2:
println("2 pressed")
default:
println("Nothing pressed")
}
}
}
老實說,我不知道爲什麼,這是行不通的。請幫我解決這個問題。
任何建議,將不勝感激。
此代碼是否被逐字複製和粘貼?因爲如果是這樣的話,你需要在sendTapped()的範圍內作爲一個獨立的函數來實現委託函數,何時它應該是這個對象的成員函數。只要嘗試將它移到「{}」的一個級別之外即可。另外我建議使用'UIAlertController'作爲UIActionSheet和UIAlertView,從iOS 8開始已經被棄用了。 – axiixc 2014-12-27 21:44:16
如果這樣可以解決你的問題,那麼你還可以做些什麼,如下所述:' - [UIView標籤]是不是你想在這裏開關,'buttonIndex'是。 – axiixc 2014-12-27 21:45:39
@axiixc這是Swift,所以「UIView.tag不是你想要在這裏打開的。」方括號語法對Swift編碼器沒有意義。 :-) – stone 2015-07-08 06:53:25