swift - 错误信息 Could not cast value type error - Swift

标签 swift uiviewcontroller uinavigationcontroller edit

最近我添加了以下代码,以便在点击编辑按钮时显示一些用户输入的数据。但是应用程序没有打开编辑 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/

相关文章:

iOS:具有许多 UIViewControllers 的 UIScrollView 中的内存管理

swift - 跳转到导航 Controller 时IOS显示状态栏 swift 3

ios - NIB 中的导航项始终为零

ios - 在没有 nib 的情况下以编程方式实例化 UIViewController

objective-c - 发送到实例 UIViewController 的无法识别的选择器

iphone - 将工具栏添加到 UITableViewController

ios - 在 swift 中为登录 View Controller 的背景添加模糊效果

arrays - 对照另一组 PFUsers SWIFT 检查一组 PFUsers

swift - 如何将供应商/产品 ID 添加到 USB 匹配字典

ios - 为什么 NSHTTPURLResponse 返回 nil? NSURLSession, swift 2.2