简体   繁体   中英

iOS - Back button on selecting a tab

I have a UITabBarController. When I select a tab other than the first, I want the tab bar hidden (which I achieved by hidesBottomBarWhenPushed) and the navigation bar to have a back button (not a plain, but default arrowed) (to get to the first tab). How do I achieve this?

I'll answer your question below, but I have to say that this is a very non-standard user interface which is mixing metaphors. If you really want to push to different view controllers, you really should not be using a tab bar. You probably should just have simple buttons to go to your secondary view controllers. I wouldn't be surprised if Apple would reject an app that used a tab bar like the way you're conceiving of it.

Anyway, to answer your question, you don't want to try to add a "back" button to the secondary views, but rather you just want to properly implement a navigation controller. Then, when you push to the secondary views, you get the back button automatically. And you also don't have to worry about silliness associated with hiding the tab bar, because when you push to another view, it will be pushed off, too.

So, consider this example, where you have a tab bar with buttons "One", "Two" and "Three". But, rather than being a proper tab bar controller configuration, you'd have something like this, where "One" is on navigation controller and has a simple tab bar user interface control on it, and if you happen to tap on the second or third tab, it will just push to the respective view. Thus, logically, it might look something like this (even if you're not using storyboards, this might help visualize the potential configuration):

示例情节提要

So, on the main view controller ("One"), you'd add a tab bar with an IBOutlet :

@property (weak, nonatomic) IBOutlet UITabBar *tabBar;

I'd then configure that tab bar in viewDidLoad , eg:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tabBar.delegate = self;
    self.tabBar.selectedItem = [self.tabBar.items objectAtIndex:0];
}

Clearly, the view controller itself would need to be a UITabBarDelegate , and you'd need a didSelectItem that would perform either a push segue or use your navigationController to pushViewController . And, clearly, you will want to change the references to @"Two" and @"Three" with the actual labels you put on your tab bar buttons, but hopefully this gives you the idea:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    if ([item.title isEqualToString:@"Two"])
    {
        // put code for pushing to second view controller here
    }
    if ([item.title isEqualToString:@"Three"])
    {
        // put code for pushing to third view controller here
    }
}

I'd also want the main view controller to make sure to set the selected item back to the first item when it reappears after you pop back:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.tabBar.selectedItem = [self.tabBar.items objectAtIndex:0];
}

Anyway, if you do it this way, there's no hiding of the tab bar (because it's no longer a controller, but rather just a user interface element on the first tab). This is probably the easiest way to implement your desired user interface.

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