首页 > 解决方案 > 无法将类型值转换为 UINavigationController swift3

问题描述

我收到以下转换错误(当我单击模拟器 [NavigationController] 中的“+”按钮时)并且无法弄清楚原因。我正在使用 Swift3。谢谢你的帮助!

MainVC.swift

        protocol AddTaskDelegate: class {
            func saveTask(by controller: AddVC, with data: [String:String])
        }


        class MainVC: UIViewController {
            //    all tasks from the database
            var tasks = [ToDoListTasks]()

            @IBOutlet weak var table: UITableView!

            //    connect with Core Data
            let managedObjectContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
    ...

//    prepare segue to send data
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "AddSegue" {
            let navigationController = segue.destination as! UINavigationController
            let childViewController = navigationController.topViewController as! AddVC

            childViewController.delegate = self as! AddTaskDelegate
        }
    }
 }

AddVC.swift

class AddVC: UIViewController {

    weak var delegate: AddTaskDelegate?
...

标签: iosswift3delegatesnavigationcontroller

解决方案


只需阅读错误消息。你是说

segue.destination as! UINavigationController

但是这个 segue 的目的地不是UINavigationController。


推荐阅读