3

I'm working on my first iPad application and using Storyboards for the first time.

I've got UITableViewController in my Storyboard that uses "Dynamic Prototypes" for custom cells.

What I want to do is programatically instantiate my UITableViewController subclasses but loading the super view controller from the Storyboard.

This is because I have a single UI but multiple subclasses for specific functionality that I need for each different instance.

This was very easy to do with .xib files, I would write the following code:

MyViewControllerSubClassA *viewControllerA = [[MyViewControllerSubClassA alloc] initWithNibName:@"generalViewControllerNib" bundle:nil];

MyViewControllerSubClassB *viewControllerB = [[MyViewControllerSubClassB alloc] initWithNibName:@"generalViewControllerNib" bundle:nil];

I know I can assign a subclass in the Storyboard editor when clicking on the View Controller but I want to set the subclass programmatically when instantiating.

This seems impossible to do with Storyboards as they are instantiated automatically.

This makes the entire concept of Storyboards seem flawed and not very OO.

If I move the View Controller out of the Storyboard and into a .xib file I lose the ability to use Dynamic & Static Prototypes cells as these are supported only when using Storyboards. Also Apple's documentation basically says use Storybaords from now on.

Camsoft
  • 11,718
  • 19
  • 83
  • 120

1 Answers1

-1

I would try something like this:

MyViewControllerSubclassA *controllerA = (MyViewControllerSubclassA *)[self.storyboard instantiateViewControllerWithIdentifier:@"myGenericVC"];
Simon Germain
  • 6,834
  • 1
  • 27
  • 42
  • 2
    While that would cast the UIViewController to MyViewControllerSubclassA it would not actually be a subclass of MyViewControllerSubclassA would it? – Camsoft Oct 10 '12 at 10:13
  • I don't think it would, but that's the closest you can get to doing what you want. Storyboards aren't really designed to do what you're trying to do. – Simon Germain Oct 10 '12 at 10:46