ios Swift 以编程方式导航到另一个视图控制器/场景
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39450124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift programmatically navigate to another view controller/scene
提问by Victor
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.)
我正在使用以下代码以编程方式导航到另一个 ViewController。它工作正常,但它如何隐藏navigation bar
. 我该如何解决?(导航栏是通过ViewController
在navigation controller
if 中嵌入 来创建的。)
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("nextView") as NextViewController
self.presentViewController(nextViewController, animated:true, completion:nil)
回答by jaiswal Rajan
Swift 5
斯威夫特 5
The default modal presentation style is a card. This shows the previous view controller at the top and allows the user to swipe away the presented view controller.
默认的模态呈现样式是卡片。这在顶部显示了之前的视图控制器,并允许用户滑动显示的视图控制器。
To retain the old style you need to modify the view controller you will be presenting like this:
要保留旧样式,您需要修改视图控制器,您将像这样呈现:
newViewController.modalPresentationStyle = .fullScreen
This is the same for both programmatically created and storyboard created controllers.
这对于以编程方式创建的和故事板创建的控制器都是相同的。
Swift 3
斯威夫特 3
With a programmatically created Controller
使用以编程方式创建的控制器
If you want to navigate to Controller created Programmatically, then do this:
如果要导航到以编程方式创建的控制器,请执行以下操作:
let newViewController = NewViewController()
self.navigationController?.pushViewController(newViewController, animated: true)
With a StoryBoard created Controller
使用 StoryBoard 创建的控制器
If you want to navigate to Controller on StoryBoard with Identifier "newViewController", then do this:
如果要使用标识符“newViewController”导航到 StoryBoard 上的 Controller,请执行以下操作:
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "newViewController") as! NewViewController
self.present(newViewController, animated: true, completion: nil)
回答by zeeshan
SWIFT 4.x
斯威夫特 4.x
The Strings in double quotes always confuse me, so I think answer to this question needs some graphical presentation to clear this out.
双引号中的字符串总是让我感到困惑,所以我认为这个问题的答案需要一些图形表示来清除它。
For a banking app, I have a LoginViewController and a BalanceViewController. Each have their respective screens.
对于银行应用程序,我有一个 LoginViewController 和一个 BalanceViewController。每个都有各自的屏幕。
The app starts and shows the Login screen. When login is successful, app opens the Balance screen.
该应用程序启动并显示登录屏幕。登录成功后,应用程序打开余额屏幕。
Here is how it looks:
这是它的外观:
The login success is handled like this:
登录成功是这样处理的:
let storyBoard: UIStoryboard = UIStoryboard(name: "Balance", bundle: nil)
let balanceViewController = storyBoard.instantiateViewController(withIdentifier: "balance") as! BalanceViewController
self.present(balanceViewController, animated: true, completion: nil)
As you can see, the storyboard ID 'balance' in small letters is what goes in the second line of the code, and this is the ID which is defined in the storyboard settings, as in the attached screenshot.
如您所见,小写字母的故事板 ID 'balance' 是代码第二行中的内容,这是在故事板设置中定义的 ID,如所附屏幕截图所示。
The term 'Balance' with capital 'B' is the name of the storyboardfile, which is used in the first line of the code.
带有大写“B”的术语“Balance”是故事板文件的名称,用于代码的第一行。
We know that using hard coded Strings in code is a very bad practice, but somehow in iOS development it has become a common practice, and Xcode doesn't even warn about them.
我们知道在代码中使用硬编码字符串是一种非常糟糕的做法,但不知何故在 iOS 开发中它已成为一种常见做法,Xcode 甚至没有警告它们。
回答by Okan Kocyigit
You should push the new viewcontroller by using current navigation controller, not present.
您应该使用当前的导航控制器(不存在)推送新的视图控制器。
self.navigationController.pushViewController(nextViewController, animated: true)
回答by Andres Paladines
According to @jaiswal Rajan in his answer. You can do a pushViewController like this:
根据@jaiswal Rajan 在他的回答中的说法。您可以像这样执行 pushViewController:
let storyBoard: UIStoryboard = UIStoryboard(name: "NewBotStoryboard", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "NewViewController") as! NewViewController
self.navigationController?.pushViewController(newViewController, animated: true)
回答by Aks
So If you present a view controller it will not show in navigation controller. It will just take complete screen. For this case you have to create another navigation controller and add your nextViewController
as root for this and present this new navigationController.
所以如果你展示一个视图控制器,它不会显示在导航控制器中。它只需要完整的屏幕。对于这种情况,您必须创建另一个导航控制器并nextViewController
为此添加您的root 用户并呈现这个新的导航控制器。
Another way is to just push the view controller.
另一种方法是推送视图控制器。
self.presentViewController(nextViewController, animated:true, completion:nil)
For more info check Apple documentation:- https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/doc/uid/TP40006926-CH3-SW96
有关更多信息,请查看 Apple 文档:- https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/doc/uid/TP40006926-CH3-SW96
回答by Pedro Berbel
OperationQueue.main.addOperation {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "Storyboard ID") as! NewViewController
self.present(newViewController, animated: true, completion: nil)
}
It worked for me when I put the code inside of the OperationQueue.main.addOperation
, that will execute in the main thread for me.
当我将代码放入OperationQueue.main.addOperation
, 这将在主线程中为我执行时,它对我有用。
回答by Bhargav
The above code works well but if you want to navigate from an NSObject class where you can not use self.present
.
上面的代码运行良好,但是如果您想从无法使用self.present
.
let storyBoard : UIStoryboard = UIStoryboard(name:"Main", bundle: nil)
if let conVC = storyBoard.instantiateViewController(withIdentifier: "SoundViewController") as? SoundViewController,
let navController = UIApplication.shared.keyWindow?.rootViewController as? UINavigationController{
navController.pushViewController(conVC, animated: true)
回答by shubham mishra
All other answers sounds good, I would like to cover my case, where I had to make an animated LaunchScreen, then after 3 to 4 seconds of animation the next task was to move to Home screen. I tried segues, but that created problem for destination view. So at the end I accessed AppDelegates's Window property and I assigned a new NavigationController screen to it,
所有其他答案听起来都不错,我想介绍一下我的情况,我必须制作动画 LaunchScreen,然后在 3 到 4 秒的动画播放后,下一个任务是移动到主屏幕。我尝试了 segue,但这给目标视图带来了问题。所以最后我访问了 AppDelegates 的 Window 属性,并为它分配了一个新的 NavigationController 屏幕,
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let homeVC = storyboard.instantiateViewController(withIdentifier: "HomePageViewController") as! HomePageViewController
//Below's navigationController is useful if u want NavigationController in the destination View
let navigationController = UINavigationController(rootViewController: homeVC)
appDelegate.window!.rootViewController = navigationController
If incase, u don't want navigationController in the destination view then just assign as,
如果以防万一,你不想在目标视图中使用 navigationController 那么只需分配为,
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let homeVC = storyboard.instantiateViewController(withIdentifier: "HomePageViewController") as! HomePageViewController
appDelegate.window!.rootViewController = homeVC