[英]UINavigationController topViewController not showing up
I'm using the code below to try to change the displayed viewController in a UINavigationController: 我正在使用下面的代码来尝试更改UINavigationController中显示的viewController:
var mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
var navigationController:UINavigationController = mainStoryboard.instantiateViewControllerWithIdentifier("navController") as UINavigationController
var homeViewController: UIViewController = mainStoryboard.instantiateViewControllerWithIdentifier("HomeView") as UIViewController
navigationController.popViewControllerAnimated(false)
navigationController.pushViewController(homeViewController, animated: false)
var topController = navigationController.topViewController as UIViewController
println("homeview id:\(homeViewController.restorationIdentifier)")
println("topview id: \(topController.restorationIdentifier)")
here is the storyboard: 这是情节提要:
First question: Why when I remove the popViewControllerAnimated function call in this exemple, the top view is still the LoginView even if I pushed the HomeView ? 第一个问题:为什么在这个示例中删除popViewControllerAnimated函数调用时,即使我按下HomeView,顶视图仍然是LoginView? (Isn't it supposed to be the last pushed view Controller ?)
(不是它应该是最后一个推送视图控制器吗?)
Second question: When I keep the popViewControllerAnimated function call I got the right top view (my topView is now HomeView as expected) but the displayed ViewController on the screen is still the loginView. 第二个问题:当我保持popViewControllerAnimated函数调用时,我得到了正确的顶视图(我的topView现在像预期的那样是HomeView),但是屏幕上显示的ViewController仍然是loginView。 Why the new TopViewController doesn't show up then ?
为什么新的TopViewController不出现呢?
thanks 谢谢
The problem is that your instantiating a new navigation controller rather than accessing the one that's automatically instantiated when your app starts up. 问题在于您实例化了一个新的导航控制器,而不是访问在应用启动时自动实例化的控制器。 Instead of this line,
代替这条线,
var navigationController:UINavigationController = mainStoryboard.instantiateViewControllerWithIdentifier("navController") as UINavigationController
you should use this, 你应该用这个
var navigationController:UINavigationController = self.view.window.rootViewController // it should just be self.window.rootViewController if this code is in the app delegate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.