错误消息无法转换值类型错误-Swift

时间:2018-12-18 02:09:23

标签: swift uiviewcontroller uinavigationcontroller edit

最近,我添加了以下代码,以使一些用户输入的数据在点击编辑按钮时出现。但是,该应用程序会冻结,而不是打开编辑视图控制器。

 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)

关于如何解决此问题的任何建议?

1 个答案:

答案 0 :(得分:1)

您的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