【问题标题】:Could not cast value of type to UINavigationController swift3无法将类型值转换为 UINavigationController swift3
【发布时间】:2026-01-18 01:30:01
【问题描述】:

我收到以下转换错误(当我单击模拟器 [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?
...

【问题讨论】:

    标签: ios swift3 delegates navigationcontroller


    【解决方案1】:

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

    segue.destination as! UINavigationController
    

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

    【讨论】:

    • 嗨,马特,感谢您的帮助。
    最近更新 更多