我尝试在TableView中添加一个滑动来编辑选项。如果用户按下编辑,则应打开一个新视图。
但是我总是在“editActionsForRowAt”的func“editActionsForRowAt”中遇到错误“if segue.identifier ==”ItemDetailsVS {“”“:致命错误:在解开可选值时意外发现nil
segue在Storyboard中具有正确的名称。有什么想法吗?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ItemDetailsVC" {
if let destination = segue.destination as? ItemDetailsViewController {
if let item = sender as? Item2 {
destination.itemToEdit = item
}
}
}
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let edit = UITableViewRowAction(style: .normal, title: "Edit"){ (action, indexPath) in
var segue: UIStoryboardSegue!
if segue.identifier == "ItemDetailsVC" {
if let objects = self.controller.fetchedObjects , objects.count > 0 {
let item = objects[indexPath.row]
self.performSegue(withIdentifier: "ItemDetailsVC", sender: item)
}
}
}
return [edit]
}
答案 0 :(得分:0)
这是因为您正在使用未初始化的Segue对象使用“?”而不是“!”。同时检查标识符。