Search code examples
iosuiviewcontrolleruitabcontroller

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.


Solution

  • 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;
    }