我正在尝试向我的操作表添加操作/事件,但是当按下按钮时,没有任何反应。 我的代码如下所示:
@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")
}
}
}
老实说,我不知道为什么这行不通。请帮我解决这个问题。
如有任何建议,我们将不胜感激。
最佳答案
弗兰克。你有正确的想法。您针对工作表标签调用单击按钮索引。而是针对按钮本身调用它:
func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
switch buttonIndex {
case 0: // your cancel button
Etc
}
}
此外,如果您为 iOS8 编写代码,则已弃用包含处理程序的 UIAlertController。容易多了。仅供引用。但是如果你让它向后兼容,它就不适用于 iOS7。
另外确保你设置你的代表
关于ios - Swift - UIActionSheet 事件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27671002/