繁体   English   中英

Swift:通过TabBar和导航控制器以编程方式从一个视图过渡到另一个视图

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

用户登录并登陆到viewNumOne上,他们填写了姓名和地址,然后看到了viewNumTwo和viewNumThree。 他们现在注销。 当他们重新登录时,我希望他们直接进入viewColorBlue(这是我遇到的问题)。

(登录屏幕)具有登录字段的View Controller。 登录后,他们进入作为TabBar的rootVC并进入第一个选项卡viewNumOne(工作正常)

(根)TabBar:

(第一个选项卡-tabBar [0])viewNumNavController> viewNumOne(名称/地址信息字段在此处)> viewNumTwo> viewNumThree

(第二个选项卡-tabBar [1])viewColorNavController> viewColorRed> viewColorBlue> viewColorWhite(注销按钮在此处)

这是我尝试的代码,但是它崩溃了:

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)

我花了20个小时才弄清楚这一点,因此希望这可以节省其他所有时间。 您需要做的是重置TabBar导航控制器的根视图控制器。

这是第一步。 假设用户登陆到viewNumOne上,他们在其中填写了姓名和地址。 假设他们正确填写了自己的姓名和地址,那么如果他们注销并重新登录,则无需再次查看此场景。 要呈现vc的新场景,您必须首先设置要更改vc的选项卡栏的导航控制器。

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

这是第二步。 您必须对希望用户看到的新视图控制器进行排列。

    //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]

现在,最后一步是使用上方的数组更改选项卡栏的根视图控制器。

//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)

希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM