I have a tab bar controller and a bunch of the same tabs. Each tab only differs in functionality, but the UI’s are all the same. In the storyboard I designed the flow and UI of one tab and set it base class. Then when I create the tabs I tried typecasting them before adding them to the tab bar but it didn’t work.
In the storyboard the View Controller indentified “TabView” has the custom class “TabColor”
TabRed *red = (TabRed *)[storyboard instantiateViewControllerWithIdentifier:@"TabView"];
TabBlue *blue = (TabBlue *)[storyboard instantiateViewControllerWithIdentifier:@"TabView"];
However the loadView method in TabColor gets called, not the TabRed/TabBlue.
Also if I nslog it the result is a TabColor object:
NSLog(@"%@", red)
Expected: TabRed
Actual: TabColor
tl;dr:
Storyboards and xibs contain collections of serialized objects. Specifying a class in a storyboard means you will get an instance of that class when you load the storyboard. A way to get the behavior you’re looking for would be to use the delegation pattern common in cocoa/cocoa-touch.
Long Version
Storyboards, and similarly xib/nib files, are actually sets of encoded objects when you get down to it. When you specify a certain view is a UICustomColorViewController in the storyboard, that object is represented as a serialized copy of that an instance of that class. When the storyboard is then loaded and
instantiateViewControllerWithIdentifier:gets called, an instance of the class specified in the storyboard will be created and returned to you. At this point you’re stuck with the object you were given, but you’re not out of luck.Since it looks like you’re wanting to do different things you could architect your view controller such that that functionality is handled by a different class using delegation.
Create a protocol to specify the functionality you’d like to be different between the two view controllers.
Add a delegate property to your viewcontroller:
And then have your new objects implement the protocol and do the thing you want them to.
Then create and hook up those objects when you load the storyboard.
This should then allow you to customize the functionality of the view controller by changing the type of object that is assigned to the controllers delegate slot.