我尝试在TableView中添加一个滑动来编辑选项 . 如果用户按下编辑,则应打开新视图 .
但是我总是在func“editActionsForRowAt”中遇到错误“if segue.identifier ==”ItemDetailsVS {“”“:致命错误:在展开Optional值时意外发现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]
}
1 回答
这是因为您正在使用未初始化的Segue对象使用“?”而不是“!” . 也检查标识符 .
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/OptionalChaining.html