Ios Xcode swift因未捕获异常而终止应用程序';NSUnknownKeyException
我有这样一个数组:Ios Xcode swift因未捕获异常而终止应用程序';NSUnknownKeyException,ios,swift,uiviewcontroller,nib,Ios,Swift,Uiviewcontroller,Nib,我有这样一个数组: let listCourse = ["Pommes", "Framboises", "Pâtes", "Tomates", "Boudoirs", "Coca", "Lait"] 它显示在TableViewCont
let listCourse = ["Pommes",
"Framboises",
"Pâtes",
"Tomates",
"Boudoirs",
"Coca",
"Lait"]
它显示在TableViewController中:
if let mycell = cell as? MyTableViewCell {
mycell.myLabel.text = self.listCourse[indexPath.row]
} // ou if cell is MyTableViewCell
return cell
}
当我单击一个项目时,它会打开一个带有所单击项目名称的ViewController,并且它应该有一个关闭此ViewController的按钮。
这是为了在单击项目时显示视图控制器:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
print("Select article \(self.listCourse[indexPath.row])")
let storyboard = UIStoryboard(name:"Main", bundle:nil)
if let controller = storyboard.instantiateViewController(withIdentifier: "ViewController") as? ViewController{
self.present(controller, animated: true){
}
controller.myDetail.text = self.listCourse[indexPath.row]
}
}
这是ViewController:
@IBAction func closeButton(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
@IBOutlet weak var myDetail: UILabel!
@IBAction func closeButton(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
@IBOutlet weak var myDetail: UILabel!
@IBOutlet weak var closeButton: UIButton!
但是,当我运行应用程序时,会出现崩溃和以下错误:
TableViewController[11561:421467]***由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[setValue:forUndefinedKey::]:此类不符合key closeButton的键值编码。”
当我删除按钮时,我不明白为什么,有人能有什么想法吗 我发现了问题,这里是我的新ViewController:
@IBAction func closeButton(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
@IBOutlet weak var myDetail: UILabel!
@IBAction func closeButton(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
@IBOutlet weak var myDetail: UILabel!
@IBOutlet weak var closeButton: UIButton!
可能重复的