无法推送到导航控制器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法推送到导航控制器相关的知识,希望对你有一定的参考价值。

 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.从“ UIViewController”投射?无关类型'DetailViewController'总是失败2.无法将类型“ DetailViewController”的值转换为预期的参数类型“ UIViewController”

答案

似乎DetailViewController不是UIViewController的子类。要解决此问题,请按如下所示修改DetailViewController定义:

class DetailViewController: UIViewController 
另一答案

还有另一种方法:将tableView单元格中的序列添加到情节提要上的视图控制器中

enter image description here

然后以这种方式在prepare(for segue)方法中配置detailViewController:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) 
    if let vc = segue.destination as? DetailViewController 
        vc.selectedImage = pictures[indexPath.row]
    

以上是关于无法推送到导航控制器的主要内容,如果未能解决你的问题,请参考以下文章

将 UITableViewController 推送到导航堆栈时看不到导航栏

使用 overCurrentContext 呈现视图控制器,然后推送到导航堆栈会导致导航栏重叠

如何检查视图控制器是不是以模态方式呈现或推送到导航堆栈上?

如何将多个视图控制器推送到导航控制器上

导航控制器有时会使用情节提要推送到黑屏

导航栏在推送到新视图控制器时会缩小