【问题标题】:Switching to a TabBar tab view programmatically?以编程方式切换到 TabBar 选项卡视图?
【发布时间】:2011-07-21 18:37:32
【问题描述】:

假设我在我的 iPhone 应用程序的一个选项卡视图中有一个 UIButton,我想让它在 TabBarController 的选项卡栏中打开另一个选项卡。我将如何编写代码来做到这一点?

我假设我卸载现有视图并加载特定选项卡视图,但我不确定如何准确编写代码。

【问题讨论】:

标签: iphone objective-c ios uitabbarcontroller


【解决方案1】:

在 Swift 或 Objective-C 中尝试此代码

斯威夫特

self.tabBarController.selectedIndex = 1

Objective-C

[self.tabBarController setSelectedIndex:1];

【讨论】:

  • 请注意,如果索引映射到更多视图控制器中的一个选项卡(如果您有五个以上的选项卡),这将不起作用。在这种情况下,请使用-setSelectedViewController:
  • 执行此操作并成功更改选项卡后,我无法再正常选择选项卡栏。更新:这在某种程度上与我在以编程方式交换标签之前调用 popToRootViewController 有关。
  • 但是如果我想切换标签,Self 将如何工作。假设我现在单击选项卡 3 并在用户按下确定时显示 alertView,我想再次切换到选项卡 1。自我不会是正确的,因为它不是当前的对象。正确吗?
  • 这很好用,但我还必须在切换选项卡时传递数据。我在切换到的标签中有一个标签,一旦我切换标签,就需要更新它。!有什么简单的解决办法吗?
  • @AdamJohns 你是如何解决你的问题的。使其表现得像本机 tabBar。它应该在第二次点击时弹出到 rootViewController。
【解决方案2】:

注意标签是从0开始索引的。所以下面的代码sn-p可以工作

tabBarController = [[UITabBarController alloc] init];
.
.
.
tabBarController.selectedViewController = [tabBarController.viewControllers objectAtIndex:4];

转到栏中的第五个选项卡。

【讨论】:

    【解决方案3】:

    我的看法是selectedIndex或使用objectAtIndex不一定是切换标签的最佳方式。如果您对标签重新排序,硬编码的索引选择可能会影响您之前的应用行为。

    如果你有想要切换到的视图控制器的对象引用,你可以这样做:

    tabBarController.selectedViewController = myViewController
    

    当然,您必须确保myViewController 确实在tabBarController.viewControllers 的列表中。

    【讨论】:

    • 我需要选择 moreNavController,据我所知这是唯一的方法
    • 是的,这个答案使我免于在项目中遇到的几乎无法解释的故障!非常感谢!
    【解决方案4】:

    您只需将 UITabBarController 上的 selectedIndex 属性设置为适当的索引,视图就会发生变化,就像用户点击选项卡按钮一样。

    【讨论】:

    • 一个小的区别是没有调用可以通知用户切换标签的委托方法。
    • 设置selectedIndex 对索引> 4 的我也不起作用,但selectedViewController 可以。
    【解决方案5】:

    我尝试了 Disco S2 的建议,结果很接近,但这最终对我有用。完成另一个选项卡中的操作后调用此方法。

    for (UINavigationController *controller in self.tabBarController.viewControllers)
    {
        if ([controller isKindOfClass:[MyViewController class]])
        {
            [self.tabBarController setSelectedViewController:controller];
            break;
        }
    }
    

    【讨论】:

      【解决方案6】:

      类似于 Stuart Clark 的解决方案,但适用于 Swift 3:

      func setTab<T>(_ myClass: T.Type) {
          var i: Int = 0
          if let controllers = self.tabBarController?.viewControllers {
              for controller in controllers {
                  if let nav = controller as? UINavigationController, nav.topViewController is T {
                      break
                  }
                  i = i+1
              }
          }
          self.tabBarController?.selectedIndex = i
      }
      

      像这样使用它:

      setTab(MyViewController.self)
      

      请注意,我的 tabController 链接到 navigationControllers 后面的 viewControllers。如果没有 navigationControllers,它看起来像这样:

      if let controller is T {
      

      【讨论】:

      • 没有循环,没有导航控制器: func selectViewController(type:ViewControllerModel.Type) { self.selectedViewController = self.viewControllers?.first(where: { viewController in viewController is ViewControllerModel }) }
      【解决方案7】:

      对于您可能要移动选项卡的情况,这里有一些代码。

      for ( UINavigationController *controller in self.tabBarController.viewControllers ) {
                  if ( [[controller.childViewControllers objectAtIndex:0] isKindOfClass:[MyViewController class]]) {
                      [self.tabBarController setSelectedViewController:controller];
                      break;
                  }
              }
      

      【讨论】:

        【解决方案8】:

        我的问题有点不同,我需要从第一个 tabBar 中的一个 childViewController 切换到第二个 tabBar 的 home viewController。我只是使用楼上提供的解决方案:

        tabBarController.selectedIndex = 2
        

        但是当它切换到第二个tabBar的主页时,内容是不可见的。当我调试时,viewDidAppear、viewWillAppear、viewDidLoad 都没有被调用。 我的解决方案是在 UITabBarController 中添加以下代码:

        override var shouldAutomaticallyForwardAppearanceMethods: Bool 
        {
            return true
        }
        

        【讨论】:

          【解决方案9】:

          我希望能够指定按类而不是按索引显示哪个选项卡,因为我认为它是一种强大的解决方案,它不太依赖于您连接 IB 的方式。我没有找到 Disco 或 Joped 的解决方案,所以我创建了这个方法:

          -(void)setTab:(Class)class{
              int i = 0;
              for (UINavigationController *controller in self.tabBarContontroller.viewControllers){
                  if ([controller isKindOfClass:class]){
                      break;
                  }
                  i++;
              }
              self.tabBarContontroller.selectedIndex = i;
          }
          

          你这样称呼它:

          [self setTab:[YourClass class]];
          

          希望这对某人有帮助

          【讨论】:

          • 这很棒!我在另一个答案中为 Swift3 重写了它。
          【解决方案10】:
          import UIKit
          
          class TabbarViewController: UITabBarController,UITabBarControllerDelegate {
          
          //MARK:- View Life Cycle
          
          override func viewDidLoad() {
              super.viewDidLoad()
          
          }
          
          override func didReceiveMemoryWarning() {
              super.didReceiveMemoryWarning()
          
          }
          
          //Tabbar delegate method
          override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
              let yourView = self.viewControllers![self.selectedIndex] as! UINavigationController
              yourView.popToRootViewController(animated:false)
          }
          
          
          
          }
          

          【讨论】:

          【解决方案11】:

          AppDelegate.m文件中使用:

          (void)tabBarController:(UITabBarController *)tabBarController
           didSelectViewController:(UIViewController *)viewController
          {
          
               NSLog(@"Selected index: %d", tabBarController.selectedIndex);
          
              if (viewController == tabBarController.moreNavigationController)
              {
                  tabBarController.moreNavigationController.delegate = self;
              }
          
              NSUInteger selectedIndex = tabBarController.selectedIndex;
          
              switch (selectedIndex) {
          
                  case 0:
                      NSLog(@"click me %u",self.tabBarController.selectedIndex);
                      break;
                  case 1:
                      NSLog(@"click me again!! %u",self.tabBarController.selectedIndex);
                      break;
          
                  default:
                      break;
          
              }
          
          }
          

          【讨论】:

            【解决方案12】:

            类似于 Stuart Clark 的解决方案,但适用于 Swift 3 并使用恢复标识符来查找正确的选项卡:

            private func setTabById(id: String) {
              var i: Int = 0
              if let controllers = self.tabBarController?.viewControllers {
                for controller in controllers {
                  if let nav = controller as? UINavigationController, nav.topViewController?.restorationIdentifier == id {
                    break
                  }
                  i = i+1
                }
              }
              self.tabBarController?.selectedIndex = i
            }
            

            这样使用(“Humans”和“Robots”也必须在 storyboard 中为特定的 viewController 和它的 Restoration ID 设置,或者使用 Storyboard ID 并选中“use storyboard ID”作为恢复 ID):

            struct Tabs {
                static let Humans = "Humans"
                static let Robots = "Robots"
            }
            setTabById(id: Tabs.Robots)
            

            请注意,我的 tabController 链接到 navigationControllers 后面的 viewControllers。如果没有 navigationControllers,它看起来像这样:

            if controller.restorationIdentifier == id {
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2018-11-20
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-04-07
              • 1970-01-01
              相关资源
              最近更新 更多