【问题标题】:how to push view controller after changing tabbar controller selectedIndex together一起更改标签栏控制器 selectedIndex 后如何推送视图控制器
【发布时间】:2020-07-13 01:44:15
【问题描述】:

我有六个 tabItem 视图控制器,每个子视图控制器都有自己的导航控制器。更改 selectedIndex 后,我无法推送新的视图控制器。我使用 AZTabBarController 创建了一个 tabbar 变量。

//the viewcontroller I want to push
let vc = self.storyboard?.instantiateViewController(withIdentifier: "CreateArticleViewController") as! CreateArticleViewController
//this can changing my tabbar selected index well.
self.currentTabBar?.setIndex(3, animated: true)
// Below code doesn't work
self.navigationController?.pushViewController(vc, animated: true)
self.currentnavigationController?.pushViewController(vc, animated: true)

tabcontroller -> 导航1 -> A1 -> A2

-> 导航 2 -> B1 -> B2 -> B3 -> B4

-> 导航 3 -> C1 -> C2 -> C3 -> C4

-> 导航 4 -> D1 -> D2 -> D3 -> D4

-> 导航 5 -> E1 -> E2 -> E3

-> 导航 6 -> F1 -> F2 -> F3

例如,当我在 A1 中点击一个按钮时,如何更改索引并将视图控制器推送到 D3? 我应该使用自定义委托来做到这一点吗? 请参阅我的故事板的快照。 https://imgur.com/a/X860m7p

【问题讨论】:

    标签: swift uitabbarcontroller uitabbar


    【解决方案1】:

    以下模式将允许您立即将屏幕显示到所需的目的地并使用配置的导航控制器(从任何地方)返回。

    tabcontroller -> 导航1 -> A1 -> A2

    func moveToA2() {
       let a1VC = #(instantiate A1 View Controller)
       let a2VC = #(instantiate A2 View Controller)
       let rootVC = 
       UIApplication.shared.windows.first?.rootViewController as? 
    UITabBarController
       let navigationController = rootVC?.children[0] as? UINavigationController
       rootVC?.selectedIndex = 0
       navigationController?.popToRootViewController(animated: false)
       navigationController?.pushViewController(a1VC, animated: false)
       navigationController?.pushViewController(a2VC, animated: false)
    }
    

    tabcontroller -> 导航2 -> B1 -> B2 -> B3 -> B4

    func moveTob4() {
        let b1VC = #(instantiate B1 View Controller)
        let b2VC = #(instantiate B2 View Controller)
        let b3VC = #(instantiate B3 View Controller)
        let b4VC = #(instantiate B4 View Controller)
        let rootVC = UIApplication.shared.windows.first?.rootViewController as? UITabBarController
        let navigationController = rootVC?.children[1] as? UINavigationController
        rootVC?.selectedIndex = 1
        navigationController?.popToRootViewController(animated: false)
        navigationController?.pushViewController(b1VC, animated: false)
        navigationController?.pushViewController(b2VC, animated: false)
        navigationController?.pushViewController(b3VC, animated: false)
        navigationController?.pushViewController(b4VC, animated: false)
    }
    

    【讨论】:

      【解决方案2】:

      当您更改 selectedIndex 时,您推送的导航是针对 A 而不是新的 D,所以您需要

      self.currentTabBar!.setIndex(3, animated: true) 
      let dNav = self.currentTabBar!.viewControllers[3] as! UINavigationController
      dNav.pushViewController(vc, animated: true) 
      

      【讨论】:

      • 它说“'AZTabBarController' 类型的值没有成员'viewControllers'”。我是不是做错了什么?
      猜你喜欢
      • 1970-01-01
      • 2013-06-22
      • 1970-01-01
      • 2012-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多