我有一个普遍的问题:我在应用中设置了一个TabBar
。当我单击项目1时,TabBar显示嵌入在ViewController
中的NavigationController
(VC 1)。当用户注销时(使用firebase和addStateDidChangeListener
),该VC应该推送另一个VC(VC 2)。它非常有用:当我注销VC1时,按VC2。但是,当我再次单击选项卡栏上的项目1时。你猜怎么了?没有用户时,我可以看到VC 1。我认为我必须以某种方式关闭VC 1,但是我不知道如何实现此功能。你能帮我吗?
答案 0 :(得分:3)
点击注销按钮后,从导航控制器中删除VC1并添加VC2
if var viewControllers = self.navigationController?.viewControllers {
viewControllers.removeLast()
viewControllers.append(VC2())
self.navigationController?.setViewControllers(viewControllers, animated: true)
}
在VC2
中再次登录时,再次更改视图控制器
self.navigationController?.setViewControllers([VC1()], animated: true)
注销
if var viewControllers = self.tabBarController?.viewControllers {
let newVC = UINavigationController(rootViewController: VC2())
newVC.tabBarItem = UITabBarItem(title: "Log In", image: nil, tag: 0)
viewControllers.removeFirst()
viewControllers.insert(newVC, at: 0)
self.tabBarController?.viewControllers = viewControllers
}
登录
if var viewControllers = self.tabBarController?.viewControllers {
let newVC = UINavigationController(rootViewController: VC1())
newVC.tabBarItem = UITabBarItem(title: "Home", image: nil, tag: 0)
viewControllers.removeFirst()
viewControllers.insert(newVC, at: 0)
self.tabBarController?.viewControllers = viewControllers
}
答案 1 :(得分:0)
您可以覆盖窗口rootviewcontroller的新标签栏控制器。因此,将来您不会在新开发(新的屏幕层次结构)中遇到任何错误。
//Login
let navigation = UINavigationController.init(rootViewController: vc1)
let tabVC = UITabBarController()
tabVC.viewControllers?.append(navigation)
self.appDelegate?.window?.rootViewController = navigation
//Logout
let navigation = UINavigationController.init(rootViewController: vc2)
let tabVC = UITabBarController()
tabVC.viewControllers?.append(navigation)
self.appDelegate?.window?.rootViewController = navigation