UITabBars and UITableViews. Keeping the Tab bar on screen when a table cell is clicked

Go To StackoverFlow.com

0

I'm trying to get a GUI with a UITabBar and UITableViews set up.

I've got a UITabView that is programmatically created. One of the Tabs displays a UITableView that is also programmatically created.

This UITableView then displays other views when didSelectRowAtIndexPath is called.

Unfortunately, when a table cell is clicked, my tab view goes away and the new table view is displayed.

What I can't get my head around is how to structure the views so that the tabBar stays on the screen.

Is it as simple as making the UITableViews shorter, or is there some window/view mojo that I'm missing?

Thanks

2012-04-05 22:24
by Alex Zavatone


4

You should use a UITabBarController to display the UITabBar rather than doing it directly.

Then use a UITableViewController as the view controller for a given tab. Though I get the impression that you want to present descendent UITableViews when a row is selected. If this is the case, you ought to use a UINavigationController as the tab bar's view controller, and let it manage your UITableViewControllers.

Remember that on iOS you really need to use the view controller pattern - the frameworks take care of a lot of things for you under the hood.


Follow-up:

OK, the following straightforward implementation works just fine for me. Please ignore the many obvious issues with this code (beginning with the fact that I hacked it all together in the application delegate!); it's intended purely as a model for how your controllers should be glued together.

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate, UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UINavigationController *navController;

@end




@implementation AppDelegate

@synthesize window = _window;
@synthesize navController = _navController;

- (void)dealloc
{
    [_navController release];
    [_window release];
    [super dealloc];
}

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

    UITableViewController *rootTVC = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
    [[rootTVC tableView] setDelegate:self];
    [[rootTVC tableView] setDataSource:self];
    [rootTVC setTitle:@"Root Table"];

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootTVC];
    [rootTVC release];
    [navController setTitle:@"My Table"];

    UIViewController *anotherViewController = [[UIViewController alloc] init];
    [anotherViewController setTitle:@"Not the table"];

    UITabBarController *tbc = [[UITabBarController alloc] init];
    [tbc setViewControllers:[NSArray arrayWithObjects:navController, anotherViewController, nil]];
    [self setNavController:navController];
    [navController release];
    [anotherViewController release];
    [[self window] setRootViewController:tbc];
    [tbc release];

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *reuseIdentifier = @"foo";
    UITableViewCell *cell = [[tableView dequeueReusableCellWithIdentifier:reuseIdentifier] retain];
    if (! cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    }
    [[cell textLabel] setText:[NSString stringWithFormat:@"Row %d", [indexPath row]]];
    return [cell autorelease];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UITableViewController *newTVC = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
    [newTVC setTitle:[NSString stringWithFormat:@"Table %d", [indexPath row]]];
    [[newTVC tableView] setDelegate:self];
    [[newTVC tableView] setDataSource:self];
    [[self navController] pushViewController:newTVC animated:YES];
}

@end
2012-04-05 22:53
by Conrad Shultz
I am using a UITabBarController. Sorry for not specifying this more clearly - Alex Zavatone 2012-04-05 23:35
Can you then elaborate on what you are doing in didSelectRowAtIndexPath: - what you are trying should just work - Conrad Shultz 2012-04-05 23:36
What you're doing seems correct. I'll have to actually set this up in a project to figure out what might be happening - Conrad Shultz 2012-04-06 00:16
Thank you immensely.. I'll be driving home. If you want, I can post the project. But I think I'm not setting up the views correctly. Will post a screenshot in a second - Alex Zavatone 2012-04-06 00:18
http://i.imgur.com/OLhJk.pn - Alex Zavatone 2012-04-06 00:20
Any progress Conrad - Alex Zavatone 2012-04-06 01:30
Sorry, I'm actually not at my computer (on iPhone right now). I was going to take a look this evening unless someone else got there first - Conrad Shultz 2012-04-06 01:49
OK, I've rewritten it again and again and again and the Tab bar works BUT now my UITableView does not display the header and does not nav to the next view when a cell is clicked. Here is the didSelectRowAtIndexPath code again:

LocationSpecificsTableViewController *lsvc = [[LocationSpecificsTableViewController alloc] initWithStyle:UITableViewStylePlain]; lsvc.office = [self wordAtIndexPath:indexPath]; [self.navigationController pushViewController:lsvc animated:YES]; [lsvc release];Alex Zavatone 2012-04-06 01:59

Hey Alex, try running the code I just posted. Does that work for you - Conrad Shultz 2012-04-06 02:35
Thanks so much Conrad. I'll review your code in the morning. I need to sleep up for another 13 hour day. Thanks so much for your time man - Alex Zavatone 2012-04-06 03:03
You're on my Christmas list Conrad. I can't get over how NICE it was that you took the time to put that together. I didn't run it, but read it and read it and compared and changed stuff and it helped me get to a point where OMG. It works. As I thought, it was a view controller thing, but I needed to see how it should have been done and compared it with a few examples of how it SHOULDN'T. Points - Alex Zavatone 2012-04-06 22:48
Glad to be of assistance. :- - Conrad Shultz 2012-04-06 22:52


0

Use UITabBarController instead of UITabBar.

2012-04-05 22:56
by MrWaqasAhmed
I am. Sorry for not specifying this - Alex Zavatone 2012-04-05 23:34
Ads