Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UINavigationController topViewController未显示_Ios_Xcode_Swift_Uinavigationcontroller_Storyboard - Fatal编程技术网

Ios UINavigationController topViewController未显示

Ios UINavigationController topViewController未显示,ios,xcode,swift,uinavigationcontroller,storyboard,Ios,Xcode,Swift,Uinavigationcontroller,Storyboard,我正在使用以下代码尝试更改UINavigationController中显示的viewController: var mainStoryboard = UIStoryboard(name: "Main", bundle: nil) var navigationController:UINavigationController = mainStoryboard.instantiateViewControllerWithIdentifier("navController") as UINaviga

我正在使用以下代码尝试更改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)")  
这是故事板:

第一个问题:为什么在本例中,当我删除popViewControllerAnimated函数调用时,即使我按下HomeView,顶视图仍然是LoginView?(它不应该是最后一个推送的视图控制器吗?
第二个问题:当我保持PopViewControllerInitiated函数调用时,我得到了正确的俯视图(我的俯视图现在是HomeView,正如预期的那样),但屏幕上显示的ViewController仍然是loginView。为什么新的TopViewController没有出现?

谢谢

问题在于您正在实例化一个新的导航控制器,而不是访问应用程序启动时自动实例化的导航控制器。而不是这条线,

var navigationController:UINavigationController = mainStoryboard.instantiateViewControllerWithIdentifier("navController") as UINavigationController
你应该用这个

var navigationController:UINavigationController = self.view.window.rootViewController // it should just be self.window.rootViewController if this code is in the app delegate

谢谢,帮了大忙。您知道我是否需要在同一个viewController的两个pushViewController()之间使用popViewControllerAnimated()吗?(为了在UINavigation堆栈中不包含同一viewController的两个实例?@grll,是的,如果您只需要一个实例,则需要在推送另一个实例之前先弹出旧的实例。