ios - 在 UITabBarController 中推送 ViewController

标签 ios swift uiviewcontroller uinavigationcontroller uitabbarcontroller

我有一个一般性问题:我在我的应用程序中设置了一个 TabBar。当我单击第 1 项时,TabBar 会显示嵌入在 NavigationController 中的 ViewController (VC 1)。这个 VC 应该在用户注销时推送另一个 VC (VC 2)(使用 firebase 和 addStateDidChangeListener)。效果很好:当我注销 VC1 推送 VC 2 时。但是,当我再次单击选项卡栏上的项目 1 时。你猜怎么了?没有用户时我可以看到 VC 1。我想我必须以某种方式关闭 VC 1,但我不知道如何实现此功能。你能帮帮我吗?

最佳答案

在导航 Controller 中更改 View Controller

当点击注销按钮时,从导航 Controller 中移除 VC1 并添加 VC2

if var viewControllers = self.navigationController?.viewControllers {
    viewControllers.removeLast()
    viewControllers.append(VC2())
    self.navigationController?.setViewControllers(viewControllers, animated: true)
}

VC2 中再次登录时再次更改 View Controller

self.navigationController?.setViewControllers([VC1()], animated: true)

在标签栏 Controller 中更改 View Controller

注销

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
}

关于ios - 在 UITabBarController 中推送 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56379415/

相关文章:

ios - XCUITests中,如何等待多个ui元素的存在?

swift - 解析注册问题

objective-c - 编译时出错 : No visible @interface declares the selector

ios - MyScript 如何将 mathwidget 添加为另一个 UIViewController 中的 subview

ios - 在没有 Segue 的情况下单击第一个 ViewController 上的按钮后更改第二个 ViewController 的标签文本

ios - 防止 iOS 使按下按钮的图像变暗

iphone - cocos2d + 加速度计问题

ios - NSArray 元素无法匹配 Swift 数组元素类型 - 带有 Swift 库的 Objective-C(图表)

ios - 带有 UIControlState 的 performSelector

ios - 两个 UIButtons 共享代码