d.yuk
d.yuk

Reputation: 809

How to jump to a certain view connected to a Tab Bar Controller from another view?

I have seen a few similar questions but I could not find one that is close enough to the one I am having now.

enter image description here

This is what I have now in my storyboard. I have a view with a number of buttons each of which is supposed to linked to one specific view (which is one of the tabs). Now I could only push to the tab view landing at the 1st tab while any of the buttons is clicked. I would like to know how I could actually jump to the specific tab from the buttons.

Please pardon my poor English, and thank you in advance.

Upvotes: 0

Views: 1243

Answers (3)

VRAwesome
VRAwesome

Reputation: 4803

Set storyboard ID of your TabBarController. As suggested by @Mayank Jain and First assign tags to your buttons, implement below method common for all buttons :

- (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;

}

Upvotes: 0

ppalancica
ppalancica

Reputation: 4277

Give the buttons the tags 100, 101, 102, 103, and 104.

Set the Identifier of the segue from the first Scene to the TabBar scene to showTabBar.

Connect each button to the same IBAction method, where you must write the code:

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

Also, override 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;
}

Upvotes: 2

Mayank Jain
Mayank Jain

Reputation: 5754

Set storyboard ID of your TabBarController. Refer image

enter image description here

Now access tabBar's object and select any view controller you want

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

Upvotes: 1

Related Questions