konyv12
konyv12

Reputation: 776

Why doesn't the tab bar show up on my view controllers?

I have the following in Interface Builder: enter image description here

The top left is my main view controller where I have 2 buttons that have segue to two UIViewControllers. These two UIViewControllers are linked with the Tab Bar Controller. However, how could I make those 2 buttons to link to specifically to one/other views? Right now it's connected specifically, but it (or something else) causes the bar tab not show up.

Is it the problem that I don't have the Tab Bar Controller connected to the main view?

Upvotes: 0

Views: 508

Answers (2)

danh
danh

Reputation: 62686

Yes, you're right that the problem occurs because the tab bar controller needs to be the destination of the segues. Fix it like this:

In IB, erase the segues from the two buttons and create two new ones, one from each button to the tab bar controller. Give each one an identifier, like buttonA from one button and buttonB from the other.

In the view controller, implement prepareForSegue for each segue understanding that the destination is a tab bar controller and that each segue requires a different tab selection...

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"buttonA"]) {
        UITabBarController *tabBarController = (UITabBarController *)segue.destinationViewController;
        tabBarController.selectedIndex = 0;
    }
    if ([segue.identifier isEqualToString:@"buttonB"]) {
        UITabBarController *tabBarController = (UITabBarController *)segue.destinationViewController;
        tabBarController.selectedIndex = 1;
    }
}

Upvotes: 1

Frankie
Frankie

Reputation: 11928

That's not quite how the tabBarController works.

I can see your initial view controller is the one on the top left, and it pushes either of the other two on the right on to the navigation stack if you push a button. But in your current setup, at no point does the tab controller itself get pushed on to the stack.

Instead, you would want to have your initial view controller push the tab bar controller on to the stack, through a button or otherwise, and the tab bar controller will display your other two view controllers as its setup to do.

Upvotes: 0

Related Questions