简体   繁体   中英

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

I have the following in Interface Builder: 在此处输入图片说明

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM