【问题标题】:I want to go to a specific tab in a UITabBarController from a view controller我想从视图控制器转到 UITabBarController 中的特定选项卡
【发布时间】:2014-03-24 18:45:30
【问题描述】:

首先,让我说我是一个绝对的初学者,所以如果这是一个愚蠢的问题,请原谅我。让我补充一下,我已经花了数小时/数天试图弄清楚如何解决这个问题 - 包括对 stackoverflow 的广泛搜索(也许答案就在某个地方,我只是现在不知道要搜索什么:).. .

但是,让我们继续:Xcode 故事板项目有一个小问题(?)。基本上我的项目是这样的:

导航控制器 -> 视图控制器 0 -> 选项卡栏控制器 -

当用户在 View Controller 0 中按下“按钮 #2”时,我希望他/她直接跳转到“View Controller 2”。

这是否可能,如果可以,应该使用什么代码,我应该把它放在哪里。

希望有人能帮助新手:)

问候, 乌尔里克

【问题讨论】:

    标签: xcode tabbarcontroller


    【解决方案1】:

    是的,这是可能的。您可以显示来自任何其他的任何视图控制器。 您应该简单地添加一个从button #2View Controller 2 的segue。 (我假设您将所有控制器都放在一个故事板中)

    更新:上面的解决方案将显示View Controller 2 本身没有标签栏控制器。

    如果没有看到实际代码,很难详细说明。有关详细信息,您可以参考以下文档:

    你可能会想出更具体的问题。

    更新 如果您想在标签栏控制器中预选所需的视图控制器,您可以使用以下代码草图。您可以在此处以编程方式启动 segue 并在 prepareForSegue:sender: 方法中进行所需的预初始化。

    static NSString * const kShowTabSegueID = @"ShowTab";
    
    @interface ViewController ()
    
    - (IBAction)buttonOnePressed;
    - (IBAction)buttonTwoPressed;
    - (IBAction)buttonThreePressed;
    
    @end
    
    @implementation ViewController
    
    - (IBAction)buttonOnePressed
    {
        [self performSegueWithIdentifier:kShowTabSegueID
                                  sender:@0];
    }
    
    - (IBAction)buttonTwoPressed
    {
        [self performSegueWithIdentifier:kShowTabSegueID
                                  sender:@1];
    }
    
    - (IBAction)buttonThreePressed
    {
        [self performSegueWithIdentifier:kShowTabSegueID
                                  sender:@2];
    }
    - (void)prepareForSegue:(UIStoryboardSegue *)segue
                     sender:(id)sender
    {
        if ([segue.identifier isEqual:kShowTabSegueID]) {
            NSNumber *indexToShow = sender;
            UITabBarController *tabBar = segue.destinationViewController;
            [tabBar setSelectedIndex:indexToShow.unsignedIntegerValue];
        }
    }
    
    @end
    

    【讨论】:

      【解决方案2】:

      如果您只是尝试以编程方式切换选项卡,那么简单如下:

      [self.tabBarController setSelectedIndex:1];

      如果我正确理解了您的流程,您将从 ViewController0 呈现 ViewController1(具有 UITabBarController)。在viewWillAppear: 中,将选项卡控制器(上面的代码)的 selectedIndex 设置为索引 1,即 ViewController2。

      编辑

      查看您的项目后,将此代码添加到您的 BrainBreaksViewController.m

      - (void)viewWillAppear:(BOOL)animated
      {
          [self.tabBarController setSelectedIndex:1];
      }
      

      我添加了这个,并在按下“按此按钮转到标签 #1”后切换到第二个标签。如果您希望能够有一个按钮来打开每个特定选项卡,请按照 Max Ks 的回答。

      【讨论】:

      • 嗨@RyanG。感谢您的建议。我试图听从你的建议,但还没有接近解决我的问题:) 我不确定我是否正确解释了我想要完成的事情,所以如果你仍然想尝试帮助我,请在 www.odensekatedralskole.dk/Files/Filer/Diverse/Brainbreaks.zip 上下载我的项目,以更好地了解我的项目是什么样的。在此先感谢... Ulrik
      • @ulrikbuchert 你能描述一下我应该在应用程序中采用的路径吗?我打开了。
      • 例如,如果您按下“关于标签 #1 的信息”,您将来到一个带有文本字段(“标签 #1 的信息 blahblahblah”)和按钮(“转到标签”的视图控制器#1”)。当用户按下此按钮时,我希望他/她转到该特定选项卡。就像现在一样,当按下按钮时,应用程序将始终转到“默认”选项卡。我希望你能明白我的意思:)
      • 你明白我的意思吗?
      【解决方案3】:

      因此,如果我理解正确,您在选项卡控制器之外有一个视图控制器,并且您想从该(外部)视图控制器导航到选项卡控制器中的第二个选项卡,如果这是问题所在,这就是我的解决方案我希望它对某人有所帮助,因为我自己花了一些时间在这个问题上。

      首先将(外部)Viewcontroller 0 连接到情节提要中的 tabbar 控制器,并使用模态 segue 并为其提供标识符 - “showTabBar”(不是作为一个选项卡,只是一个模态 segue)。

      然后: 在 Viewcontroller 0 中声明:

      var tabBarIndex: Int?
      
      //function that will trigger the **MODAL** segue
      private func loadTabBarController(atIndex: Int){
           self.tabBarIndex = atIndex
           self.performSegue(withIdentifier: "showTabBar", sender: self)
      }
      
      //in here you set the index of the destination tab and you are done
      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      
           if segue.identifier == "showTabBar" {   
               let tabbarController = segue.destination as! UITabBarController
               tabbarController.selectedIndex = self.tabBarIndex!       
           }         
      }
      

      然后你可以像这样导航到 ViewController 2(不要忘记它是 0 索引):

      self.loadTabBarController(atIndex: 1)
      

      截至我发布此答案的那一天,这已经过测试和工作 斯威夫特 3.0.2 / Xcode 8.2.1

      【讨论】:

      • 这个答案正好解决了我正在寻找的东西!做得好,感谢您的解释
      • 没有SEGUE我们如何做到这一点。
      • @KumarLav 您可以随心所欲地展示或推送您的 UITabBarController,就在您这样做之前,将所选索引设置为所需的索引。例如:让 tabController = MyCustomTabController() , tabController.selectedIndex = 2 , self.present(tabController, animated: true)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-15
      • 1970-01-01
      • 1970-01-01
      • 2017-12-13
      相关资源
      最近更新 更多