Swift ios 设置新的根视图控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33374272/
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 ios set a new root view controller
提问by user2722667
I wonder if its possible to set a new root VC?
我想知道是否可以设置一个新的根 VC?
My app gets init with a uinavigation controller that has a table view to be the root VC.
我的应用程序使用 uinavigation 控制器进行初始化,该控制器具有作为根 VC 的表视图。
Then from the table view I am running another segue to a login window (present modally) If you then login you end up in the redVC/account page. What I want to do now is to set the red VC to be the new root VC of the app, and remove all underlying VC's. So that I can show a menu button/icon instead of a "Back" button
然后从表视图中,我正在运行另一个登录窗口(以模态显示)如果您然后登录,您最终会进入红色的VC/帐户页面。我现在想要做的是将红色 VC 设置为应用程序的新根 VC,并删除所有底层 VC。这样我就可以显示菜单按钮/图标而不是“返回”按钮
I have found this but I dont understand how to use it:
我找到了这个,但我不明白如何使用它:
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let yourViewController: ViewController = storyboard.instantiateViewControllerWithIdentifier("respectiveIdentifier") as! ViewController
let navigationController = self.window?.rootViewController as! UINavigationController
navigationController.setViewControllers([yourViewController], animated: true)
But I cannot get it to work. So is it possible to make the red vc in the picture act as the new root VC.
但我无法让它工作。那么有没有可能让图中红色的vc充当新的根VC呢?
回答by Vashum
Swift 4.2
斯威夫特 4.2
May be you should try this
也许你应该试试这个
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let redViewController = mainStoryBoard.instantiateViewController(withIdentifier: "respectiveIdentifier") as! ViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = redViewController
回答by footyapps27
Swift 3 Update:-
斯威夫特 3 更新:-
let testController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "testController") as! TestController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = testController
回答by Sandeep Maurya
Swift 4, 5, 5.1
斯威夫特 4、5、5.1
let story = UIStoryboard(name: "Main", bundle:nil)
let vc = story.instantiateViewController(withIdentifier: "NewViewController") as! NewViewController
UIApplication.shared.windows.first?.rootViewController = vc
UIApplication.shared.windows.first?.makeKeyAndVisible()
回答by Tushar Korde
UINavigationController has a viewControllers property, which is a NSArray, And you can replaced it with your own NSArray of view controllrs.
UINavigationController 有一个 viewControllers 属性,它是一个 NSArray,你可以用你自己的视图控制器 NSArray 替换它。
This can be done as show in below sample code.
这可以按照以下示例代码所示完成。
let newViewController = self.storyboard?.instantiateViewControllerWithIdentifier("YourViewControllerollerID") as! YourViewController
let customViewControllersArray : NSArray = [newViewController]
self.navigationController?.viewControllers = customViewControllersArray as! [UIViewController]
And if you want to show this new root view controller you can just call UINavigationController's popToRootViewController() method.
如果你想显示这个新的根视图控制器,你可以调用 UINavigationController 的 popToRootViewController() 方法。
self.navigationController?.popToRootViewControllerAnimated(true)
回答by Akila Wasala
Swift 4 Answer
斯威夫特 4 个回答
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "YourViewController") as! YourViewController
let navigationController = UINavigationController(rootViewController: nextViewController)
let appdelegate = UIApplication.shared.delegate as! AppDelegate
appdelegate.window!.rootViewController = navigationController
回答by Amelia
In order to get the code snippet from the original question to work, I had to make a change to the third line
为了使原始问题中的代码片段起作用,我不得不对第三行进行更改
let navigationController = self.navigationController!
I am using this code in an @IBAction in the view controller that precedes the new root view controller.
我在新根视图控制器之前的视图控制器中的 @IBAction 中使用此代码。
Using the original code, I was receiving an error saying that my view controller had no member called window
. After looking at the documentation, I could find no property named window
. I'm wondering if the original block of code above was intended to be used inside a UINavigationController
file.
使用原始代码,我收到一条错误消息,指出我的视图控制器没有名为window
. 查看文档后,我找不到名为window
. 我想知道上面的原始代码块是否打算在UINavigationController
文件中使用。
Here is the block in its entirety.
这是整个块。
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let todayViewController: TodaysFrequencyViewController = storyboard.instantiateViewControllerWithIdentifier("todaysFrequency") as! TodaysFrequencyViewController
let navigationController = self.navigationController!
navigationController.setViewControllers([todayViewControl ler], animated: true)
回答by Caleb
I wonder if its possible to set a new root VC?
我想知道是否可以设置一个新的根 VC?
Yes,it's possible. How you do it depends on the context...
是的,这是可能的。你怎么做取决于上下文......
My app gets init with a uinavigation controller that has a table view to be the root VC.
我的应用程序使用 uinavigation 控制器进行初始化,该控制器具有作为根 VC 的表视图。
There are actually two things that are commonly called the "root view controller":
实际上有两个东西通常被称为“根视图控制器”:
UIWindow
has arootViewController
property, which is writeable.UINavigationController
has norootViewController
property, but it does have an initializer called-initWithRootViewController:
. You can set the nav controller's "root" view controller by setting it'sviewControllers
property.
UIWindow
有一个rootViewController
属性,它是可写的。UINavigationController
没有rootViewController
属性,但它确实有一个名为 的初始值设定项-initWithRootViewController:
。您可以通过设置它的viewControllers
属性来设置导航控制器的“根”视图控制器。
It sounds like you're trying to change the window's root view controller, but the code you show only changes the nav controller's viewControllers
property. Try setting the window's rootViewController
property directly. Understand, however, that if you take that approach then the navigation controller will go away too. If you want to keep the nav controller, on the other hand, go with your current approach.
听起来您正在尝试更改窗口的根视图控制器,但您显示的代码仅更改导航控制器的viewControllers
属性。尝试rootViewController
直接设置窗口的属性。但是,请理解,如果您采用这种方法,那么导航控制器也会消失。另一方面,如果您想保留导航控制器,请使用您当前的方法。
But I cannot get it to work. So is it possible to make the red vc in the picture act as the new root VC.
但我无法让它工作。那么有没有可能让图中红色的vc充当新的根VC呢?
More information here would be helpful. What do you mean by "cannot get it to work"? What happens, and what do you expect to happen?
此处的更多信息会有所帮助。“无法让它工作”是什么意思?会发生什么,你预计会发生什么?
回答by Deepti Raghav
You can use this bit of code:
你可以使用这段代码:
let newViewController = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
let customViewControllersArray : NSArray = [newViewController]
self.navigationController?.viewControllers = customViewControllersArray as! [UIViewController]
self.navigationController?.pushViewController(newViewController, animated: true)
回答by Chathuranga Silva
You can use this code when you click the login button :-
您可以在单击登录按钮时使用此代码:-
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var vc = mainStoryboard.instantiateViewControllerWithIdentifier("respectiveIdentifier") as ViewController
UIApplication.sharedApplication().keyWindow.rootViewController = vc
回答by ArturOlszak
How and from where are you presenting redVC?
您将如何以及从何处展示 redVC?
You could make it root view controller of your UINavigationController, so you would still have ability to push and pop view controllers.
您可以将其设为 UINavigationController 的根视图控制器,因此您仍然可以推送和弹出视图控制器。
self.navigationController?.viewControllers = [self];