Swift以编程方式导航到另一个视图控制器/场景 [英] Swift programmatically navigate to another view controller/scene
问题描述
我正在使用以下代码以编程方式导航到另一个ViewController。它工作正常,但有些如何隐藏导航栏
。 如何解决此问题?(导航栏是通过在导航控制器中嵌入
如果这很重要。) ViewController
创建的
I'm using following code to programmatically navigate to another ViewController. It works fine, but it some how hides the navigation bar
. How do I fix this? (the navigation bar is created by embeding the ViewController
in the navigation controller
if that matters.)
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("nextView") as NextViewController
self.presentViewController(nextViewController, animated:true, completion:nil)
推荐答案
在Swift 3中
使用以编程方式创建的控制器
如果要导航到以编程方式创建的Controller,请执行以下操作:
If you want to navigate to Controller created Programmatically, then do this:
let newViewController = NewViewController()
self.navigationController?.pushViewController(newViewController, animated: true)
使用StoryBoard创建控制器
如果你想用标识符newViewController导航到StoryBoard上的Controller,那么这样做:
If you want to navigate to Controller on StoryBoard with Identifier "newViewController", then do this:
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "newViewController") as! NewViewController
self.present(newViewController, animated: true, completion: nil)
这篇关于Swift以编程方式导航到另一个视图控制器/场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!