我已将一个按钮添加到特定的 UITableViewCell。当我选择按钮时,我遇到崩溃:
按钮点击
libc++abi.dylib:以 NSException 类型的未捕获异常终止
在 cellForRowAt
的开头,我定义了按钮:
let myButton = UIButton(type: .custom)
myButton.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
myButton.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
myButton.tintColor = UIColor.yellow()
对于indexpath.row,我附加按钮如下:
cell.accessoryView = myButton 作为 UIView
并且操作 buttonTapped
尝试加载不同的 ViewController。
我得到了按钮操作有效的确认(调用了例程)。 例程如下:
func buttonTapped() {
print("ButtonTapped")
let myPickerController = self.storyboard?.instantiateViewController(withIdentifier: "picker") as? MyPickerController
print("1")
self.present(myPickerController!, animated: true)
print("2")
}
正如您从日志中看到的,我确实看到例程被调用,但在崩溃之前我没有看到打印值 1 或 2。有人看到我做错了什么吗?
最佳答案
添加目标,例如,
myButton.addTarget(self, action: #selector(YourControllerName.buttonTapped(_:)), for: .touchUpInside)
然后更改你的函数,例如,
func buttonTapped(sender : UIButton){
....
}
希望这对您有帮助。
关于ios - 如何将目标操作添加到 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38091678/