adamteale
adamteale

Reputation: 1000

UINavigationController app to a UITabBarController app

I have been working on a UINavigationController based application, using a UITableView for the rootview and Core Data for the data source.

Unfortunately I didn't plan ahead very well and now would like to implement tab bar navigation to the app on top of what I already have.

Can anyone recommend a simple way to do this? Or am I better off starting again with the TabBar based template and try to plugin my existing code?

Thanks guys!

Adam

Upvotes: 0

Views: 569

Answers (3)

Kristopher Johnson
Kristopher Johnson

Reputation: 82545

Rather than start over, you may just want to create a tab-bar-based app and look at the code it produces, then try to do the same thing in your app.

You probably just need to change your application delegate's application:didFinishLaunchingWithOptions: method to look something like this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    NSMutableArray *viewControllers = [[[NSMutableArray alloc] init] autorelease];

    // First tab
    MyController *myController = [[[MyController alloc] init] autorelease];
    mapViewController.navigationItem.title = @"First";
    UINavigationController *myRootController = [[[UINavigationController alloc]
                                              initWithRootViewController:myController]
                                             autorelease];
    myRootController.tabBarItem.title = @"First";
    myRootController.tabBarItem.image = [UIImage imageNamed:@"MyControllerTab"];
    [viewControllers addObject:myRootController];

    // Second tab
    MyOtherController *myOtherController = [[[MyOtherController alloc] init] autorelease];
    myOtherController.navigationItem.title = @"Second";
    UINavigationController *otherRootController = [[[UINavigationController alloc] initWithRootViewController:myOtherController] autorelease];
    otherRootController.tabBarItem.title = @"Second";
    otherRootController.tabBarItem.image = [UIImage imageNamed:@"OtherControllerTab"];
    [viewControllers addObject:otherRootController];

    // Create other tabs
    // ...

    // Tab bar
    UITabBarController *tabBarController = [[[UITabBarController alloc] init] autorelease];
    [tabBarController setViewControllers:viewControllers];

    [self.window setRootViewController:tabBarController];
    [self.window makeKeyAndVisible];
    return YES;
}

Upvotes: 2

Jordan
Jordan

Reputation: 21760

Adam,

Not sure how much coding you've done already, but if you're not that familiar with connecting things in IB and setting delegates you might be better off starting over with a TabBarController template.

Here's what you do:

  1. Define new IBOutlet for UITabBarController in AppDelegate and Add TabBarController to IB
  2. Connect UITabBarController to IBOutlet in AppDelegate in IB
  3. Set Delegate for UITabBarController in IB to AppDelegate
  4. Change rootViewController to tabBarController in AppDelegate
  5. Drag All ViewControllers into Tab Bar Controller in IB
  6. Setup other Tabs using NavigationControllers or UIViewControllers depending on your design
  7. Run, Fix, Repeat

Upvotes: 0

Tim
Tim

Reputation: 60140

If you want to plug in your existing navigation controller to one of the tabs on a tab bar, it's pretty simple - just define a new UITabBarController to serve as the root controller and have it load your navigation controller into a single tab. Then change your app delegate to load the tab bar controller instead of your navigation controller.

If, however, you want to pull out multiple controllers from your existing navigation stack, you might have to do a bit more work - find places where you call pushViewController:animated: to modify your navigation stack, and instead plug those view controller instances into tabs on your tab bar. Here's where it might be worthwhile to start from a fresh tab bar template and copy over existing controllers, depending on the complexity of your code.

Upvotes: 0

Related Questions