簡體   English   中英

無法推送到導航 controller

[英]Unable to push to navigation controller

 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    // 1: try loading the "Detail" view controller and typecasting it to be DetailViewController
    if let vc = storyboard?.instantiateViewController(withIdentifier: "Detail") as? DetailViewController {
        // 2: success! Set its selectedImage property
        vc.selectedImage = pictures[indexPath.row]

        // 3: now push it onto the navigation controller
        navigationController?.pushViewController(vc, animated: true)
    }
}

我面臨問題 - 1.Cast from 'UIViewController?' 到不相關的類型 'DetailViewController' 總是失敗 2.無法將類型 'DetailViewController' 的值轉換為預期的參數類型 'UIViewController'

似乎 DetailViewController 不是UIViewController的子類。 您可以通過修改DetailViewController定義來簡單地解決此問題,如下所示:

class DetailViewController: UIViewController {
...
}

從'UIViewController' 投射? 到不相關的類型 'DetailViewController' 總是失敗

確保您的DetailViewControllerUIViewController的子類

class DetailViewController: UIVieController {

Also you can use another way to do that and avoid storyboard?.instantiateViewController(withIdentifier:) method (and avoid possible mistakes with storyboard ID): add a segue from prototype tableView cell to your view controller on a storyboard

在此處輸入圖像描述

然后在擁有tableView的 viewController 的prepare(for segue)方法中配置detailViewController

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let vc = segue.destination as? DetailViewController {
        //configure vc properties here
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM