ios - 如何将目标操作添加到 UITableViewCell

标签 ios swift uitableview

我已将一个按钮添加到特定的 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/

相关文章:

ios - 我可以创建一个我所有的 View Controller 都可以访问的类吗

iphone - 使用Button Font Awesome 图标无法正确显示?而是显示怪异的图标

ios - 当表格 View 单元格中的条件发生变化时,自动布局约束会中断

swift - 类 'ViewController' 没有初始化程序 - 类似的问题无法解决问题

ios - 无法为 UITableView 创建框架

Swift:如何访问 UITableView 中的单元格

ios - 尝试将应用程序提交到应用程序商店时 Xcode 崩溃

ios - UIButton 句柄触摸然后通过?

ios - 转换后关闭 ViewController 以释放内存

swift - 如何在 Vapor 应用程序中启用 GZip?