praks5432
praks5432

Reputation: 7792

Pushing view onto viewcontroller in tab controller

So I'm trying to convert a single view application into a tabbed application. My use case is this - in one of the view controllers I want to push a new view controller and still have the tab underneath.

I'm currently doing this -

  [self.tabBarController setViewControllers:@[self.searchViewController, self.loginViewController]];
  [self.searchViewController presentViewController:self.searchViewController.detailController animated:YES completion:nil];

However, this makes the tab across the bottom disappear.

What should I do?

Upvotes: 1

Views: 210

Answers (2)

Mike
Mike

Reputation: 9835

There are two primary methods for view navigation, the first is a presentation which displays a view from the bottom that slides up, and the second is a push which displays a view from the right that slides in from the side.

In most cases, the view I am going to display and what action kicked off the navigation determine which method I will use. For example, if I have a table view that has a list of music albums and I want to search for a song by a particular artist, to see the songs within that album I want to PUSH the view controller, i.e. slide to the right. This gives me the built-in (and intuitive) ability to go back via the automatically added back button on the navigation bar in case the song I was looking for wasn't in the album I selected.

If perhaps I wanted to present the user with the ability to edit the album details, such as renaming the album, this is a totally different type of action, and I would want to PRESENT such a view modally, i.e. from the bottom.

The major distinction between the two is where are you going and what are you doing. If the next view you are going to show is something that does one action and you are then returned back to the original view, presenting modally from the bottom is conventional. If you are doing to be potentially navigation further and further into subsections and will be coming back and forth between said subsections, like Artist->Album->Song etc., you are going to want to push the view from the side, just like the default music app in iOS does.

This is an example starter project I created that demonstrates an easy way to make this work the way you likely want. I create instances of the different view controllers I want to be contained in the tabBarController, which are associated with the tabs, and then "wrap" them each with their own navigation controller before adding them to the tabBar via the .items property. This way each view controller has its own navigation hierarchy and within each you'll be able to call [self.navigationController pushViewController:] or [self.navigationController presentViewController] to keep the navigation 'within' the views and separate from the tabBar itself.

#import "AppDelegate.h"
#import "TabBarViewController.h"
#import "InfoViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];

    InfoViewController *firstVC = [[InfoViewController alloc] init];
    firstVC.title = @"First";
    firstVC.view.backgroundColor = [UIColor redColor];
    UINavigationController *firstNC = [[UINavigationController alloc] initWithRootViewController:firstVC];

    InfoViewController *secondVC = [[InfoViewController alloc] init];
    secondVC.title = @"Second";
    secondVC.view.backgroundColor = [UIColor blueColor];
    UINavigationController *secondNC = [[UINavigationController alloc] initWithRootViewController:secondVC];

    TabBarViewController *tabBarVC = [[TabBarViewController alloc] init];
    tabBarVC.viewControllers = @[firstNC, secondNC];

    self.window.rootViewController = tabBarVC;

    [self.window makeKeyAndVisible];
    return YES;
}

That resulted in the following:

enter image description here

enter image description here

Hope that helps!

Upvotes: 1

jsd
jsd

Reputation: 7703

presentViewController is a "modal" presentation - the presented view controller takes over the entire screen. If you want to remain within the tab but move between view controllers, the root view controller in the tab should be a UINavigationController. You can then push/pop view controllers onto that.

Upvotes: 1

Related Questions