I want to go to a specific tab in a UITabBarController from a view controller
Asked Answered
K

3

6

First of all, let me say I'm an absolute beginner, so please forgive me if this is a stupid questions to be asking. And let me just add that I've spent hours/days trying to figure out how to solve this problem - including extensive searches on stackoverflow (maybe the answer is somewhere and I just don't now exactly what to search for :)...

But, let's continue: I have a small (?) problem with an Xcode storyboard project. Basically my project looks like this:

Navigation Controller -> View Controller 0 -> Tab Bar Controller -< View Controller 1, View Controller 2, View Controller 3.

When the user pushes 'button #2' in the View Controller 0, I'd like him/her to jump directly to 'View Controller 2'.

Would that be possible at all, and if so what code should use and excatly where should I put it.

Hope someone out there will help a newbie out :)

Regards, Ulrik

Kovacs answered 24/3, 2014 at 18:45 Comment(0)
S
6

Yes it is possible. You may show any view controller from any other. You should simply add a segue from button #2 to View Controller 2. (I assume you have all your controllers in single storyboard)

Update: the above solution will show you View Controller 2 itself without tab bar controller.

Hard to tell in details without seeing the actual code. For more details you may refer to these documents:

Probably you'll come up with more concrete question.

Update If you want to preselect desired view controller inside tabbar controller you may use the following code sketch. Here you can programmatically initiate a segue and do the desired pre-initialization inside prepareForSegue:sender: method.

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
Situs answered 24/3, 2014 at 19:13 Comment(0)
I
5

So if I understand correctly you have a view controller outside the tabbar controller and you want to navigate to the second tab in the tabbar controller from that (outside)view controller, if that is the problem then, this is my solution and I hope it helps someone as I spent some time on this issue myself.

First connect the (outside)Viewcontroller 0 to the tabbar controller in the storyboard with modal segue and give it identifier - "showTabBar" (not as one of the tabs just a modal segue).

Then: in Viewcontroller 0 declare:

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!       
     }         
}

then you can navigate to ViewController 2 like that(don't forget it is 0 indexed):

self.loadTabBarController(atIndex: 1)

This is tested and working as of the day I am posting this answer using Swift 3.0.2 / Xcode 8.2.1

Irritating answered 21/2, 2017 at 18:11 Comment(3)
This answer solved exactly what I was looking for! Well done and thanks for the explanationSpaceport
HOW CAN WE DO IT WITHOUT SEGUE.Placeman
@KumarLav you can present or push your UITabBarController however you wish, just before you do that set the selected index to the desired one. for example: let tabController = MyCustomTabController() , tabController.selectedIndex = 2 , self.present(tabController, animated: true)Irritating
A
4

If you are simply trying to programatically switch tabs, its as simple as:

[self.tabBarController setSelectedIndex:1];

If I am understanding your flow correctly, from ViewController0 you would present ViewController1(that has a UITabBarController). In the viewWillAppear: set the selectedIndex for the tab controller (code above) to index 1, which would be ViewController2.

EDIT

After looking at your project, add this code to your BrainBreaksViewController.m

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

I added this, and it switches to the 2nd tab after pressing "Press this button to goto tab #1". Follow Max Ks answer if you would like to be able to have a button to open each specific tab.

Avirulent answered 24/3, 2014 at 19:22 Comment(4)
Hi @RyanG. Thanks for your suggestions. I've tried to follow your advice but haven't gotten closer to solving my problem :) I'm not sure whether I've correctly explained what I'm trying to accomplish, so if you'd still want to try to help me, please download my project on www.odensekatedralskole.dk/Files/Filer/Diverse/Brainbreaks.zip to get a better idea of what my project looks like. Thanks in advance... UlrikKovacs
@Kovacs Could you describe the path I should take in the app? I have it open.Avirulent
If you for instance press the 'information about tab #1' you'll come to a view controller with a textfield ("Information about tab #1 blahblahblah") and a button ("Go to tab #1"). When the users pushes this button I want him/her to go to that specific tab. As it is now, the app will always go to the 'default' tab' when the button is pressed. I hope you can understand what I mean :)Kovacs
do you see what I mean?Kovacs

© 2022 - 2024 — McMap. All rights reserved.