James Kerstan
James Kerstan

Reputation: 323

Table view in Tab Bar App

I have been looking for weeks now and had no luck trying to find a tutorial for Xcode 4 showing how to add a Table View to a Tab Bar app. I was wondering if you could point me in the right direction?

Thanks

Upvotes: 1

Views: 1977

Answers (1)

bryanmac
bryanmac

Reputation: 39306

Any TabBarController tutorial should do because you add UIViewControllers to the tab bar. For the table view, simply create a UITableViewController. You should be able to add that to the tab bar controller ... or any other view controller. For example, if you find some other tutorial doing a TabBar with a navigationController ... simply replace the navigationController part of the tutorial with a UITableViewController. There's also plenty of docs and tutorials on UItableViewControllers.

For example, if you look at this code in an app delegate didfinishLaunchingWithOptions. Pior to this, a MyTableViewController was created (UITableViewController) and some other UIViewController.

// View Controllers for tabController - could be UItableViewControllers or any
// other UIViewController.  You will add this to the tabController
NSMutableArray *viewControllers = [[NSMutableArray alloc] init];

MyTableViewController *myTable = [[MyTableViewController alloc] initWithNibName:@"MyTableViewController" bundle:nil];
[viewControllers addObject:myTable];

SomeOtherUIViewController *other = [[SomeOtherUIViewController alloc] initWithNibName:@"SomeOtherUIViewController" bundle:nil];
[viewControllers addObject:other];    

// add the UIViewControllers to the tabController
[tabController setViewControllers:viewControllers];

// add tabbar and show
[[self window] addSubview:[tabController view]];
[self.window makeKeyAndVisible];
return YES;

And then in each of those view controllers that you added to the tabbar, make sure you add the tabbar item to them in their init

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
    {
        UITabBarItem *barItem = [[UITabBarItem alloc] 
                             initWithTitle:@"Progress" 
                             image:[UIImage imageNamed:@"report.png"] tag:2];

        [self setTabBarItem:barItem];
        [barItem release];
    }
    return self;
}

Upvotes: 2

Related Questions