2014-12-27 49 views
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") 
     } 
    } 

} 

老實說,我不知道爲什麼,這是行不通的。請幫我解決這個問題。

任何建議,將不勝感激。

+0

此代碼是否被逐字複製和粘貼?因爲如果是這樣的話,你需要在sendTapped()的範圍內作爲一個獨立的函數來實現委託函數,何時它應該是這個對象的成員函數。只要嘗試將它移到「{}」的一個級別之外即可。另外我建議使用'UIAlertController'作爲UIActionSheet和U​​IAlertView,從iOS 8開始已經被棄用了。 – axiixc 2014-12-27 21:44:16

+0

如果這樣可以解決你的問題,那麼你還可以做些什麼,如下所述:' - [UIView標籤]是不是你想在這裏開關,'buttonIndex'是。 – axiixc 2014-12-27 21:45:39

+0

@axiixc這是Swift,所以「UIView.tag不是你想要在這裏打開的。」方括號語法對Swift編碼器沒有意義。 :-) – stone 2015-07-08 06:53:25

回答

0

Frank。你有正確的想法。您對照片標籤調用單擊的按鈕索引。相反,把它靠在按鈕本身:

func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) { 
switch buttonIndex { 
    case 0: // your cancel button 
Etc 
} 
} 

此外,如果您對iOS8上的編碼這種模式已經過時了UIAlertController包含處理程序。很容易。只是FYI。但是如果你使它向後兼容,它不適用於iOS7。

此外請確保您的設置您的代表