最近我添加了以下代码,以便在点击编辑按钮时显示一些用户输入的数据。但是应用程序没有打开编辑 View Controller ,而是卡住了。
override func tableView(_ tableView: UITableView,
leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
let modifyAction = UIContextualAction(style: .normal, title: "Edit", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("Update action ...")
let MainStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let vc = MainStoryboard.instantiateViewController(withIdentifier: "FreshReleaseEdit") as! EditfreshreleaseViewController
vc.selectedFreshRelease = self.freshreleases[indexPath.row]
self.present(vc, animated: true, completion: nil)
success(true)
})
modifyAction.title = "Edit"
modifyAction.backgroundColor = .blue
return UISwipeActionsConfiguration(actions: [modifyAction])
}
当我点击编辑按钮时,应用程序崩溃并显示以下错误消息:
更新操作... 无法将类型“UINavigationController”(0x1d37e81e0)的值转换为“fresh_release.EditfreshreleaseViewController”(0x1043d7248)。 2018-12-17 20:56:30.547305-0500 新版本 [7644:1985252] 无法将类型“UINavigationController”(0x1d37e81e0)的值转换为“fresh_release.EditfreshreleaseViewController”(0x1043d7248)。 (lldb)
关于如何解决这个问题有什么建议吗?
最佳答案
您的 EditfreshreleaseViewController
很可能嵌入到 UINavigationController
中。这就是您的类型转换不起作用的原因。
需要先将VC转为UINavigationController
,再将topViewController
转为EditfreshreleaseViewController
。
改变行:
let vc = MainStoryboard.instantiateViewController(withIdentifier: "FreshReleaseEdit") as! EditfreshreleaseViewController
到
let navVC = MainStoryboard.instantiateViewController(withIdentifier: "FreshReleaseEdit") as! UINavigationController
let vc = navVC.topViewController as! EditfreshreleaseViewController
关于swift - 错误信息 Could not cast value type error - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53825472/