robertmiles3
robertmiles3

Reputation: 919

Manually add more tabs to UITabController with Storyboard and Monotouch

I am currently using the new iOS 5 Storyboard approach to creating my Tabbed Application with Monotouch. I have developed two of my tab views in Xcode with Storyboard and linked them appropriately to the Tab Bar Controller. I also want to develop (in Xcode) a third tab view that would be shared among two additional tabs. I want to reuse the same layout, but display different data depending on which tab is selected (think something like a "Popular" and a "Recent" that would have the same layout but different data).

To do this, I figured I could add the tab manually twice after the Storyboard-driven tabs are added. How do I do this with the Storyboard approach? I'm not sure where in the code to do this since the loading of the Storyboard seems pretty transparent (i.e. no code in AppDelegate that I see). Or, is there another (easier/better) way to share a view between two tabs using the Storyboard approach?

Upvotes: 2

Views: 884

Answers (1)

allaire
allaire

Reputation: 6045

I don't know Monotouch, but here's how I did it in Objective-c. I didn't find anything about this topic, so if something is wrong, people please comment :) By the way, I'm using ARC, so I don't manually manage memory! What I needed to achieve was like you, having a tab bar, loading the same viewController, but loading different data for each tab.

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{   
    UITabBarController *root = (UITabBarController*)self.window.rootViewController;

    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];

    TeamViewController *home = [[mainStoryboard instantiateViewControllerWithIdentifier:@"Team"] initHome];
    TeamViewController *visitor = [[mainStoryboard instantiateViewControllerWithIdentifier:@"Team"] initVisitor];

    [root setViewControllers:[NSArray arrayWithObjects:home, visitor, nil] animated:NO];

    UITabBar *tabs = root.tabBar;
    UITabBarItem *homeTab = [tabs.items objectAtIndex:0];
    UITabBarItem *visitorTab = [tabs.items objectAtIndex:1];

    homeTab.title = @"Home team";
    visitorTab.title = @"Visitor team";

    return YES;
}

You can see I call initHome and initVisitor when I load my two TeamViewController, here is the code about it.

TeamViewController.h

@interface TeamViewController : UIViewController
{
    enum
    {
        HOME,
        VISITOR
    };

    int team;
}

TeamViewController.m

- (id)initHome
{
    team = HOME;

    return self;    
}

- (id)initVisitor
{
    team = VISITOR;

    return self;    
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    if(team == HOME)
    {
        label.text = @"home data";
    }
    else if(team == VISITOR)
    {
        label.text = @"visitor data";
    }
}

I don't know how well you can translate that to your project, but I hope you get the big picture of it :)

If you need to read a bit about how to access the first view controller using the storyboard: http://developer.apple.com/library/ios/#releasenotes/Miscellaneous/RN-AdoptingStoryboards/_index.html#//apple_ref/doc/uid/TP40011297 There is a section called "Accessing the First View Controller"

Upvotes: 0

Related Questions