【问题标题】:How to pass data from VC to another by skipping tab bar controller如何通过跳过标签栏控制器将数据从 VC 传递到另一个
【发布时间】:2018-08-25 10:40:13
【问题描述】:

我有一个主 VC,它通过 segue 连接到选项卡栏控制器,并且该选项卡栏控制器有 2 个部分,它们是 2 个 ViewController。我需要将数据从 Main VC 传递到使用 segue 从 Tab Bar Controller 派生的特定 VC。由于我只能使用一个 segue(仅显示 TBController),有没有办法通过跳过 Tab Bar 控制器将数据直接从 main 传递到另一个?

附言。这两个 VC 在 Tab Bar Controller 和它们自己之间也有一个导航控制器连接。

【问题讨论】:

    标签: ios swift uitabbarcontroller segue uistoryboardsegue


    【解决方案1】:

    试试这个

      var secondTab = self.tabBarController?.viewControllers[1] as SecondViewController
      secondTab.array = firstArray 
    

    【讨论】:

    • 不,这种方式行不通。无法将“UITabBarController”类型的值转换为“UINavigationController”..
    【解决方案2】:

    在准备 segue 功能时,您可以通过 TabBarControllers 属性 viewControllers 获取与 tabBar 关联的控制器 - 这让您可以选择标签栏控制器的视图控制器之一成为活动视图控制器。然后你使用 tabBarController 的属性 selectedIndex 来真正地与那个 VC 连接。

      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
                let index = 0//the index of the tab you wish to segue to
                if let bar = segue.destination as? TabBarVC {
                   if let nav = bar.viewControllers[index] as?  UINavigationController {
                     if let navChildVC = navigationController.viewControllers[0] as? YourVCType { // index of the controller of enumerate to find it
                 // pass data to navChildVC
                       bar.selectedIndex = index
               }
            }
         }
    }
    

    【讨论】:

    • 我不能像这样用nav.somedata =somedata 传递数据,因为它是导航控制器
    • 与标签栏使用相同:让 navChildVC = navigationController.viewControllers[0] 我也编辑了我的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-14
    • 1970-01-01
    相关资源
    最近更新 更多