【问题标题】:Access UITabBarController embedded in UIViewController访问嵌入在 UIViewController 中的 UITabBarController
【发布时间】:2019-08-14 11:05:05
【问题描述】:

我在 UIViewController 中嵌入了一个 UITabBarController。我正在尝试从父 UIViewController 访问 UITabBarController 实例。

vc.children 的返回类型为 [UIViewController],因此 UITabBarController 不会出现。

vc.tabBarController 为 nil,因为视图未嵌入 TabBarController。我的设置正好相反。

有什么想法吗?

我的故事板:

【问题讨论】:

    标签: ios swift uiviewcontroller storyboard uitabbarcontroller


    【解决方案1】:

    我需要在vc.children 之前移动window?.rootViewController = viewController

    【讨论】:

      【解决方案2】:

      最快的解决方案:
      vc.children.compactMap({$0 as? UITabBarController}).first

      最佳解决方案:
      从情节提要中选择Embed Segue 并给出一个标识符(比如“containerEmbedSegue”。接下来,在您的 vc 中:

      var tabBarVC: UITabBarController?
      
      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
          if segue.identifier == "containerEmbedSegue" {
              self.tabBarVC = segue.destination as? UITabBarController
          }
      }
      

      【讨论】:

      • 第一个解决方案不起作用,因为 vc.children 是一个空数组。
      • 第二种解决方案似乎更有希望,但我无法让它发挥作用。我正在尝试从 AppDelegate 访问 TabBarController。我生成了一个父 ViewController 的实例,然后尝试访问嵌入的 TabBarController。但是,prepare() 永远不会被调用。我试过打电话给vc.performSegue(withIdentifier: "containerEmbedSegue"),但应用程序崩溃并出现以下错误:*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'containerView is required.'
      • @Daniel 您从哪里尝试访问 vc.children?
      • 在 AppDelegate func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
      • 你如何获得第一个vc的实例?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-25
      • 1970-01-01
      • 2015-03-20
      相关资源
      最近更新 更多