如何从另一个视图跳转到连接到Tab Bar Controller的某个视图?

时间:2014-08-06 05:16:12

标签: ios uiviewcontroller uitabcontroller

我已经看到了一些类似的问题,但我找不到一个与我现在所拥有的问题足够接近的问题。

enter image description here

这就是我现在在故事板中的内容。我有一个带有多个按钮的视图,每个按钮都应链接到一个特定视图(这是一个选项卡)。现在,我只能在单击任何按钮时按下第一个选项卡上的选项卡视图着陆。我想知道如何从按钮跳转到特定选项卡。

请原谅我可怜的英语,并提前感谢你。

3 个答案:

答案 0 :(得分:2)

为按钮指定标签100,101,102,103和104。

将segue的标识符从第一个Scene设置为TabBar场景to showTabBar。

将每个按钮连接到相同的IBAction方法,您必须在其中编写代码:

- (IBAction)onButtonTap:(id)senderButton
{
    [self performSegueWithIdentifier:@"showTabBar" sender:senderButton];
}

另外,覆盖prepareForSegue:sender ::

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.

    UITabBarController *tabBarController = segue.destinationViewController;

    tabBarController.selectedIndex = ((UIButton *) sender).tag - 100;
}

答案 1 :(得分:1)

设置TabBarController的故事板ID。 参考图片

enter image description here

现在访问tabBar的对象并选择您想要的任何视图控制器

UITabBarController *tabBar=[MainStoryboard instantiateViewControllerWithIdentifier:@"MyTabBarController"];
tabBar.selectedIndex = 2;

答案 2 :(得分:0)

设置TabBarController的故事板ID。正如@Mayank Jain和 首先为按钮分配标签,实现以下所有按钮的常用方法:

- (IBAction)buttonTaped:(UIButton *)sender
{
    MainTabBarViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"MainTabBarView"];

    if (sender.tag == 0)
        vc.selectedIndex = 0;

    else if (sender.tag == 1)
        vc.selectedIndex = 1;

    else if (sender.tag == 2)
        vc.selectedIndex = 2;

    else if (sender.tag == 3)
        vc.selectedIndex = 3;

    else
        vc.selectedIndex = 4;

}

相关问题