Ios 在Swift 3中以编程方式转到另一个视图

Ios 在Swift 3中以编程方式转到另一个视图,ios,swift,swift3,Ios,Swift,Swift3,我使用以下代码以编程方式转到swift 3中的另一个视图。运行时没有错误。但我不知道为什么它不会变成那样 我使用的代码: let images = self.storyboard?.instantiateViewController(withIdentifier:"Collection") as! UICollectionViewController self.navigationController?.pushViewController(images, animate

我使用以下代码以编程方式转到swift 3中的另一个视图。运行时没有错误。但我不知道为什么它不会变成那样

我使用的代码:

let images = self.storyboard?.instantiateViewController(withIdentifier:"Collection") as! UICollectionViewController
            self.navigationController?.pushViewController(images, animated: true)
我想转到CollectionView.swift


我认为最好使用Segues,而不是通过编程方式调用控制器的脚本ID。你可以找到如何使用它们

但是如果您必须使用SBID,下面是我的一个项目中的一个示例片段

let vc = self.storyboard?.instantiateViewController(withIdentifier: "UpdateFilesViewController")
self.navigationController?.present(vc!, animated: true, completion: nil)

请注意,演示可能是您追求的解决方案,而不是推动。此外,您似乎正在将UIViewController强制转换为UICollectionViewController,这也可能导致问题。请注意,UICollectionViewController是UIViewController的子类,因此不需要强制转换。

我们在评论中发现,您当前的viewController没有navigationController

这意味着

self.navigationController?.pushViewController(images, animated: true)
什么也不做。您需要使用navigationController设置初始viewController,以使其生效。

更改情节提要标识符 您只需要在identity inspector中将您的故事板标识符更改为“CollectionOfImages”作为Storybroad标识,这样它就可以继续了


注意:请确保您当前的视图控制器嵌入了导航控制器,否则它在移动过程中不会推送新控制器。

如果您不想使用导航控制器,您只需使用以下方式演示控制器:

let images = self.storyboard?.instantiateViewController(withIdentifier:"Collection") as! UICollectionViewController

self.present(images, animated: true, completion: nil)

要在视图控制器之间导航,请使用UINavigationController

我将为您提供一个导航的基本示例,希望它能帮助您在项目中实现导航


结果 ViewController在导航之间将图像传递给DetailViewController


设置您的视图 首先,确保根控制器嵌入了导航控制器控件,以便可以使用segues进行导航:

连接用于导航的视图


代码

您当前的viewController是否有navigationController?不,它没有navigationcontrollerhey@GijoVarghese,您可能首先遇到一些基本的容器视图问题。提纲。。根据您最近的更新,您似乎正在搜索名为“CollectionOfImages”的标识符,但在stroyboard中,您说它名为“Collection”。确保这些匹配。不,我只是重命名了它以测试您使用的图像中第一张图像中提供的序列是什么?你能在编辑中描述你的故事板元素吗。我将尝试在测试项目上进行复制。但我不需要在我的应用程序中显示导航栏。任何方式?不,您可以隐藏导航栏。获取错误:“由于未捕获的异常'NSInternalInconsistencyException'终止应用程序,原因:'-[UICollectionViewController loadView]实例化了视图控制器,标识符为“Collection”,来自情节提要“Main”,但未获取UICollectionView。”
class ViewController: UIViewController {

  @IBOutlet weak var imageView: UIImageView!
  
  override func viewDidLoad() {
    super.viewDidLoad()
  }
  
  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    
    // showDetail Segue
    if segue.identifier == "showDetail" {
      // Sending the image to DetailViewController
      // Before appears in the screen.
      let detailViewController = segue.destination as! DetailViewController
      detailViewController.image = sender as? UIImage
    }
    
  }

  @IBAction func loginButton(_ sender: AnyObject) {
    
    // Go to another view controller
    performSegue(withIdentifier: "showDetail", sender: imageView.image)
    
  }

}
class DetailViewController: UIViewController {
  @IBOutlet weak var imageView: UIImageView!
  var image: UIImage?
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    if let imageSent = image {
      imageView.image = imageSent
    }
  }
}