Swift: Programmatically transitioning from 1 view to another via a TabBar and Navigation Controller

北战南征 提交于 2019-12-24 05:47:11

问题


The user logs in and lands on viewNumOne, they fill out their name and address and get to see viewNumTwo and viewNumThree. They now logout. When they log back in I want them to go straight to viewColorBlue (this is where I am having the problem).

(Login Screen) View Controller with login fields. Once logged in they go to the rootVC which is a TabBar and they land on the first tab which is viewNumOne (this works fine)

(Root) TabBar:

(First Tab - tabBar[0]) viewNumNavController > viewNumOne (name/address info fields are here) > viewNumTwo > viewNumThree

(Second Tab - tabBar[1]) viewColorNavController > viewColorRed > viewColorBlue > viewColorWhite (logout button is here)

Here is the code I tried but it crashes:

let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

let tabBarController = mainStoryboard.instantiateInitialViewController() as! UITabBarController
tabBarController.selectedIndex = 1

let viewColorNaviCon = tabBarController.viewControllers![1] as! UINavigtionController
let viewColorBlueVC = viewColorNaviCon.topViewController as! ViewColorBlueController
self.presentViewController(viewColorBlueVC, animated: true, completion: nil)

回答1:


It took me 20 hrs to figure this out so hopefully this will save someone else all that time. What you need to do is reset the TabBar's Navigation Controller's root view controller.

This is the first step. Assume the user landed on viewNumOne where they filled out their name and address. Assuming they properly filled out their name and address, if they logged out and logged back in there wouldn't be any need for them to see this scene again. To present a new scene of vc's you have to first set the tab bar's navigation controller that you want to change the vc's for.

let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 
let tabBarController = mainStoryboard.instantiateViewControllerWithIdentifier("MainTabBarController"
tabBarController.selectedIndex = 1
let viewNumNavController  = tabBarController.viewControllers![1] as! ViewNumNavigationController

This is the second step. You have to make an array of the new view controllers you want the user to see.

    //Array of view controllers you want to set as the Navigation Controller's new array of VC's
let viewColorRedVC = mainStoryboard.instantiateViewControllerWithIdentifier("ViewColorRedController") as! ViewColorRedController
let viewColorBlueVC = mainStoryboard.instantiateViewControllerWithIdentifier("ViewColorBlueController") as! ViewColorBlueController
let viewColorWhiteVC = mainStoryboard.instantiateViewControllerWithIdentifier("ViewColorWhiteController") as! ViewColorWhiteController
let newArrayOfVCs = [viewColorRedVC, viewColorBlueVC, viewColorWhiteVC]

Now the last step is to change the tab bar's root view controller using the array from above.

//This method is what sets the Navigation Controller's new child views to be presented
viewNumNavController.setViewControllers(newArrayOfVCs, animated: false)
//This method is what sets the exact view controller you want use as the actual root vc (the very first scene the user will see)
viewNumNavController.popToViewController(viewColorRedVC, animated: true)
self.presentViewController(tabBarController, animated: true, completion: nil)

Hope this helps!



来源:https://stackoverflow.com/questions/37907772/swift-programmatically-transitioning-from-1-view-to-another-via-a-tabbar-and-na

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!