Question

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

Was it helpful?

Solution

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

OTHER TIPS

Use UITabBarController instead of UITabBar.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top