ios - Xcode 继续 "Could not cast value of type"

标签 ios swift xcode

我有转至两个不同的 ViewControllers,Detail 和 Search。转到 Detail 的 segues 工作正常,但是转到 Search 的 segues 使应用程序崩溃。我花了两个小时阅读类似的问题,但似乎都没有相同的问题:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    let detailVC = segue.destination as! DetailViewController

    if segue.identifier == "newDocSegue"   {
        // Create a new document and pass it to the detail view.
        detailVC.doc = Document()
        detailVC.isAddAction = true
    }

    if segue.identifier == "editDocSegue"    {
        // Load the existing document into the detail view, for editing.
        let indexPath = tableView.indexPath(for: sender as! UITableViewCell)!
        detailVC.doc = Document[indexPath.row]
        detailVC.isAddAction = false
    }
     else if segue.identifier == "searchSegue" {
        shouldPerformSegue(withIdentifier: "searchSegue", sender: Any?.self)
    }

}

最佳答案

你的问题是

让 detailVC = segue.destination as!细节 View Controller

行。这将尝试将任何 VC(包括您的搜索 VC)转换为 DetailViewController。试试这个:

let detailVC : UIViewController

if segue.identifier == "newDocSegue"   {
    // Create a new document and pass it to the detail view.
    detailVC = segue.destination as! DetailViewController
    detailVC.doc = Document()
    detailVC.isAddAction = true
}

if segue.identifier == "editDocSegue"    {
    // Load the existing document into the detail view, for editing.
    detailVC = segue.destination as! ???  // not sure what type you want here
    let indexPath = tableView.indexPath(for: sender as! UITableViewCell)!
    detailVC.doc = Document[indexPath.row]
    detailVC.isAddAction = false
}
 else if segue.identifier == "searchSegue" {
    detailVC = segue.destination as! SearchViewController
    shouldPerformSegue(withIdentifier: "searchSegue", sender: Any?.self)
}

关于ios - Xcode 继续 "Could not cast value of type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42198254/

相关文章:

ios - Swift UITextField adjustmentFontSizeToFitWidth 无法正常工作

swift - SwiftUI 应用程序的 NavigationView 中的 onAppear 和 onDisappear 是否按预期运行?

ios - 将 XML 文件从站点获取到电话

ios UIScrollView 有一个错误的默认 contentOffset - swift

ios - 当我尝试使用 userDefaults 保存到 ui 颜色时,Xcode 给我错误 : Fatal error: Unexpectedly found nil while unwrapping an Optional value

ios - LLVM Xcode 版本 4.6.1 (4H512) 的编译器错误

iPhone : How to separate string like "ListName|Definition|Answer"

ios - CoreData属性可以扩展吗?

iphone - xcode:如何在使用 AVFoundation 录制音频后保存音频文件

ios - 无法识别的选择器使用通知发送到 ViewController 中的实例