Swift:通过 TabBar 和导航控制器以编程方式从一个视图转换到另一个视图 [英] Swift: Programmatically transitioning from 1 view to another via a TabBar and Navigation Controller

查看:15
本文介绍了Swift:通过 TabBar 和导航控制器以编程方式从一个视图转换到另一个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

(登录屏幕)查看带有登录字段的控制器.登录后,他们会转到 TabBar 的 rootVC,然后他们会进入第一个选项卡,即 viewNumOne(这很好用)

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

(根)标签栏:

(第一个标签 - tabBar[0])viewNumNavController > viewNumOne(名称/地址信息字段在这里)> viewNumTwo > viewNumThree

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

(第二个标签 - tabBar[1])viewColorNavController > viewColorRed > viewColorBlue > viewColorWhite(注销按钮在这里)

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

推荐答案

我花了 20 个小时才弄明白这个问题,所以希望这能一直拯救其他人.您需要做的是重置 TabBar 的导航控制器的根视图控制器.

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.

这是第一步.假设用户登陆 viewNumOne,在那里他们填写了他们的姓名和地址.假设他们正确填写了他们的姓名和地址,如果他们退出并重新登录,他们就不需要再次看到这一场景.要呈现 vc 的新场景,您必须首先设置要更改 vc 的标签栏导航控制器.

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)

希望这会有所帮助!

这篇关于Swift:通过 TabBar 和导航控制器以编程方式从一个视图转换到另一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆