Launch Storyboard from within Nib?

Go To StackoverFlow.com

2

I am looking at rebuilding the settings section of my app using the new functionality provided by storyboards. Not wanting to touch the rest of my app at this point, so my main NIB will be staying.

Now when going from my NIB'ed tabBar to another NIB I just add a viewController to the tabBar in IB and then set the NIB Name property to the NIB which I want to load when that tab is pressed.

But there is no 'storyboard name' property that I can see, so how is this done?

2012-04-04 16:39
by trapper


3

There is no "official" way to do it at the moment, but you can do it using some tricks.

1) add your view controller to your tabbar in nib in the usual way. Leave the nib field empty.

2) create your storyboard and add your viewcontroller. Set the class and set a storyboard ID (I'll use "theID" for this example)

3) add a static bool var to your .m file, outside implementation or interface

static BOOL aFlag = NO;

4) in your viewcontroller class override this method:

- (id) awakeAfterUsingCoder:(NSCoder *)aDecoder
{
    if (!aFlag){
        aFlag = YES;
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
        return [storyboard instantiateViewControllerWithIdentifier:@"theID"];
    } else {
        return self;
    }
}

essentially:

  • when you load the object from the tab bar nib, a first call to "initWithCoder" is made, and the object loads without nib
  • after initWithCoder, awakeAfterUsingCoder is called and there you substitute the object with another loaded from storyboard. Object of the same class but archived in the storyboard
  • when you load the object from the storyboard, another call to both initWithCoder and awakeAfterUsingCoder. You use the flag to avoid a loop and return self (at the second call, the object is loaded from storyboard so returning self is ok)

I tried and it works good ;-) If you want here is an example project: http://www.lombax.it/files/testTabNib.zip

2012-11-11 20:52
by LombaX
it worked, but for some reason the TabBarItem's image has disappeared... any idea why - Kof 2013-02-10 09:58
The Tab Bar Item (the button that appears on the Tab Bar) is a property of the View Controller, not of the Tab Bar. So, you have to add a Tab Bar Item to the new ViewController in storyboard. You can do it programmatically or, simply, graphically. Drag/drop a new Tab Bar Item from the object library under the ViewController (as a child - LombaX 2013-02-10 10:21
Ads