1. <i id='jxIlQ'><tr id='jxIlQ'><dt id='jxIlQ'><q id='jxIlQ'><span id='jxIlQ'><b id='jxIlQ'><form id='jxIlQ'><ins id='jxIlQ'></ins><ul id='jxIlQ'></ul><sub id='jxIlQ'></sub></form><legend id='jxIlQ'></legend><bdo id='jxIlQ'><pre id='jxIlQ'><center id='jxIlQ'></center></pre></bdo></b><th id='jxIlQ'></th></span></q></dt></tr></i><div id='jxIlQ'><tfoot id='jxIlQ'></tfoot><dl id='jxIlQ'><fieldset id='jxIlQ'></fieldset></dl></div>

      <legend id='jxIlQ'><style id='jxIlQ'><dir id='jxIlQ'><q id='jxIlQ'></q></dir></style></legend>
        <bdo id='jxIlQ'></bdo><ul id='jxIlQ'></ul>
      <tfoot id='jxIlQ'></tfoot>

        <small id='jxIlQ'></small><noframes id='jxIlQ'>

      1. 如何使用标签栏管理容器视图中的控制器

        时间:2023-06-10

          1. <small id='L3USi'></small><noframes id='L3USi'>

              <tfoot id='L3USi'></tfoot>

              <i id='L3USi'><tr id='L3USi'><dt id='L3USi'><q id='L3USi'><span id='L3USi'><b id='L3USi'><form id='L3USi'><ins id='L3USi'></ins><ul id='L3USi'></ul><sub id='L3USi'></sub></form><legend id='L3USi'></legend><bdo id='L3USi'><pre id='L3USi'><center id='L3USi'></center></pre></bdo></b><th id='L3USi'></th></span></q></dt></tr></i><div id='L3USi'><tfoot id='L3USi'></tfoot><dl id='L3USi'><fieldset id='L3USi'></fieldset></dl></div>
                <bdo id='L3USi'></bdo><ul id='L3USi'></ul>

                    <tbody id='L3USi'></tbody>
                  <legend id='L3USi'><style id='L3USi'><dir id='L3USi'><q id='L3USi'></q></dir></style></legend>
                  本文介绍了如何使用标签栏管理容器视图中的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在我的故事板上,我有主 ViewController,而不是 TabBarViewController,它由底部的 TabBar、顶部的视图和顶部的 ContainerView 组成中间.ContainerView 有一个 NavigationController.我也有 4 个 ViewControllers,其中之一 - NavigationControllerRootViewController.我希望在选择 TabBarItem 时显示 ViewControllers 之一,以后我将添加幻灯片菜单,该菜单也将显示选定的 ViewController.
                  我有下一个代码,它只显示 ContainerView 中的初始 ViewController,当我选择 TabBarItems 时,新的 ViewControllers 没有显示,我只看到第一个 View Controller.出了什么问题?

                  On my storyboard I have main ViewController, not TabBarViewController, which consist of TabBar on the bottom, view on the top and ContainerView on the middle. ContainerView have a NavigationController. I also have 4 ViewControllers, one of them - RootViewController of NavigationController. I wish to show one of ViewControllers when I selecting TabBarItem, and in future I will add slide menu, which also will show selected ViewController.
                  I have next code, which only shows initial ViewController inside ContainerView, and when I selecting TabBarItems, new ViewControllers don't showing and I see only first View Controller. What goes wrong?

                  class ViewController: UIViewController {
                  
                      @IBOutlet weak var container: UIView!
                      @IBOutlet weak var first: UITabBarItem!
                      @IBOutlet weak var second: UITabBarItem!
                      @IBOutlet weak var third: UITabBarItem!
                      @IBOutlet weak var fours: UITabBarItem!
                      @IBOutlet weak var tabBar: UITabBar!
                  
                      var firstVC: FirstViewController?
                      var secondVC: SecondViewController?
                      var thirdVC: ThirdViewController?
                      var foursVC: FoursViewController?
                  
                      var navi: UINavigationController?
                  
                  
                  
                  
                      override func viewDidLoad() {
                          super.viewDidLoad()
                          tabBar.delegate = self
                          initialSetup()
                      }
                  
                      func initialSetup() {
                  
                          tabBar.selectedItem = tabBar.items?.first
                  
                          navi = self.storyboard?.instantiateViewController(withIdentifier: "containerNavi") as? UINavigationController
                  
                          firstVC = self.storyboard?.instantiateViewController(withIdentifier: "FirstViewController") as? FirstViewController
                          secondVC = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController
                          thirdVC = self.storyboard?.instantiateViewController(withIdentifier: "ThirdViewController") as? ThirdViewController
                          foursVC = self.storyboard?.instantiateViewController(withIdentifier: "FoursViewController") as? FoursViewController
                      }
                  
                      func showVC(number: Int) {
                          switch number {
                          case 0:
                              navi?.popToRootViewController(animated: true)
                              print("0")
                          case 1:
                              if let second = secondVC {
                                  navi?.pushViewController(second, animated: true)
                              }
                              print("1")
                          case 2:
                              if let third = thirdVC {
                                  navi?.pushViewController(third, animated: true)
                              }
                              print("2")
                          case 3:
                              if let fours = foursVC {
                                  navi?.pushViewController(fours, animated: true)
                              }
                              print("3")
                          default:
                              return
                          }
                      }
                  
                  
                  }
                  
                  extension ViewController: UITabBarDelegate {
                  
                      func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
                          showVC(number: item.tag)
                      }
                  
                  }
                  

                  故事板截图:

                  推荐答案

                  你可以尝试使用这个扩展来添加/删除任何4到containerView

                  You can try to use this extesion to add/remove any of the 4 to containerView

                  extension UIViewController {
                      func add(_ child: UIViewController, frame: CGRect? = nil) {
                          addChildViewController(child)
                          if let frame = frame {
                              child.view.frame = frame
                          }
                          view.addSubview(child.view)
                          child.didMove(toParentViewController: self)
                      }
                      func remove() {
                          willMove(toParentViewController: nil)
                          view.removeFromSuperview()
                          removeFromParentViewController()
                  
                     }
                  
                  }
                  

                  //像这样使用它

                  let vc = self.storyboard?.instantiateViewController(withIdentifier: "first")
                  
                  self.add(vc, frame: self.containerView.frame)
                  

                  删除

                  vc.remove()
                  

                  这篇关于如何使用标签栏管理容器视图中的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:将 UITabBarController 与 UINavigationController 一起使用 - Swift 3 下一篇:iOS11 中奇怪的 uitableview 行为.带有导航推送动画的单元格向上滚动

                  相关文章

                  <tfoot id='C5fNX'></tfoot>
                1. <legend id='C5fNX'><style id='C5fNX'><dir id='C5fNX'><q id='C5fNX'></q></dir></style></legend>

                  1. <i id='C5fNX'><tr id='C5fNX'><dt id='C5fNX'><q id='C5fNX'><span id='C5fNX'><b id='C5fNX'><form id='C5fNX'><ins id='C5fNX'></ins><ul id='C5fNX'></ul><sub id='C5fNX'></sub></form><legend id='C5fNX'></legend><bdo id='C5fNX'><pre id='C5fNX'><center id='C5fNX'></center></pre></bdo></b><th id='C5fNX'></th></span></q></dt></tr></i><div id='C5fNX'><tfoot id='C5fNX'></tfoot><dl id='C5fNX'><fieldset id='C5fNX'></fieldset></dl></div>

                      <small id='C5fNX'></small><noframes id='C5fNX'>

                      • <bdo id='C5fNX'></bdo><ul id='C5fNX'></ul>