将按钮放在 tableview 单元格上时崩溃,无法识别的选择器
Crashed when putting button on tableview cell, unrecognized selector
PatientTableViewCell viewBtn:]: unrecognized selector sent to instance
0x7fdde280ac00
libc++abi.dylib: terminating with uncaught exception of type
NSException
有什么想法吗?谢谢
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let current_patient = patients[indexPath.row]
let cell = myTable.dequeueReusableCellWithIdentifier("patientCell", forIndexPath: indexPath) as! PatientTableViewCell
let type = current_patient.enrollable_type
var unique_id = current_patient.unique_id
cell.patientTypelbl.text = type
cell.patientIDlbl.text = unique_id
cell.viewBtn.tag = indexPath.row
cell.viewBtn.addTarget(self, action: "viewDetailAction:", forControlEvents: .TouchUpInside)
return cell
}
@IBAction func viewDetailAction(sender: UIButton){
print("clicked")
self.performSegueWithIdentifier("patientDetailCell", sender: self)
}
对于患者 Tableviewcell:
class PatientTableViewCell: UITableViewCell {
@IBOutlet weak var viewBtn: UIButton!
@IBOutlet weak var titleType: UILabel!
@IBOutlet weak var titleID: UILabel!
@IBOutlet weak var patientTypelbl: UILabel!
@IBOutlet weak var patientIDlbl: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
实际上 @kennytm 所说的是真的,但目前只是一个警告,但最终您将需要使用语法:action : #selector(YourViewController.yourFunctionName)
。
您的问题是您向 viewBtn
添加了一个 viewDetailAction
的目标,但这必须是一个函数,而不是情节提要中的一个动作。
你可以只声明这个函数:
func viewDetailAction() {
print("clicked")
self.performSegueWithIdentifier("patientDetailCell", sender: self)
}
并删除您的操作:
@IBAction func viewDetailAction(sender: UIButton){
print("clicked")
self.performSegueWithIdentifier("patientDetailCell", sender: self)
}
这里有两个选择
在您的单元格中为行索引路径注释以下行,并检查按钮操作是否存在。
cell.viewBtn.addTarget(self, action: "viewDetailAction:", forControlEvents: .TouchUpInside)
使用您提到的代码并重命名按钮操作方法名称
“@IBAction”
您的错误是在 xCode 编辑器中设置了 IBAction,并以编程方式将选择器目标添加到同一按钮..
首先从 Referencing Outlets 中删除您的 IBAction
设置,如图中所示:
之后,您必须通过删除 IBAction 标签重命名您的函数:
func viewDetailAction() {...}
PatientTableViewCell viewBtn:]: unrecognized selector sent to instance 0x7fdde280ac00
libc++abi.dylib: terminating with uncaught exception of type NSException
有什么想法吗?谢谢
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let current_patient = patients[indexPath.row]
let cell = myTable.dequeueReusableCellWithIdentifier("patientCell", forIndexPath: indexPath) as! PatientTableViewCell
let type = current_patient.enrollable_type
var unique_id = current_patient.unique_id
cell.patientTypelbl.text = type
cell.patientIDlbl.text = unique_id
cell.viewBtn.tag = indexPath.row
cell.viewBtn.addTarget(self, action: "viewDetailAction:", forControlEvents: .TouchUpInside)
return cell
}
@IBAction func viewDetailAction(sender: UIButton){
print("clicked")
self.performSegueWithIdentifier("patientDetailCell", sender: self)
}
对于患者 Tableviewcell:
class PatientTableViewCell: UITableViewCell {
@IBOutlet weak var viewBtn: UIButton!
@IBOutlet weak var titleType: UILabel!
@IBOutlet weak var titleID: UILabel!
@IBOutlet weak var patientTypelbl: UILabel!
@IBOutlet weak var patientIDlbl: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
实际上 @kennytm 所说的是真的,但目前只是一个警告,但最终您将需要使用语法:action : #selector(YourViewController.yourFunctionName)
。
您的问题是您向 viewBtn
添加了一个 viewDetailAction
的目标,但这必须是一个函数,而不是情节提要中的一个动作。
你可以只声明这个函数:
func viewDetailAction() {
print("clicked")
self.performSegueWithIdentifier("patientDetailCell", sender: self)
}
并删除您的操作:
@IBAction func viewDetailAction(sender: UIButton){
print("clicked")
self.performSegueWithIdentifier("patientDetailCell", sender: self)
}
这里有两个选择
在您的单元格中为行索引路径注释以下行,并检查按钮操作是否存在。 cell.viewBtn.addTarget(self, action: "viewDetailAction:", forControlEvents: .TouchUpInside)
使用您提到的代码并重命名按钮操作方法名称 “@IBAction”
您的错误是在 xCode 编辑器中设置了 IBAction,并以编程方式将选择器目标添加到同一按钮..
首先从 Referencing Outlets 中删除您的 IBAction
设置,如图中所示:
之后,您必须通过删除 IBAction 标签重命名您的函数:
func viewDetailAction() {...}