2014-12-27 50 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。

此外请确保您的设置您的代表