1

Is it possible to use one storyboard setup of a view controller with two view controllers?

Situation: I created one view controller's view in my storyboard. This one lets the user add a new data entry. Lets call it MyNewEntryViewController. This works fine. Now I need a way to edit my data entries. For that I'd just like to subclass my first view controller and adapt it where it needs to be. Lets call this one MyEditEntryViewController.

I would load MyNewEntryViewController using instantiateViewController… on my storyboard:

MyNewEntryViewController *newEntryViewController = [storyboard instantiateViewControllerWithIdentifier:@"NewEntryViewController"];

That works great.

What if I want to instantiate my new view controller now? Trying the obvious

MyEditEntryViewController *editEntryViewController = [storyboard instantiateViewControllerWithIdentifier:@"NewEntryViewController"];

results in an MyNewEntryViewController stored in my variable because that's what I had defined in that storyboard.

So, whats the best way to work with my storyboard definition and be able to use two different view controllers? How do you guys do this?

Thanks!
–f

flohei
  • 5,116
  • 10
  • 34
  • 59
  • 1
    This seems to be a similar question: http://stackoverflow.com/questions/14111572/how-to-use-single-storyboard-uiviewcontroller-for-multiple-subclass. – Martin R Jul 21 '13 at 09:04

1 Answers1

1

No this is not possible. It is possible to do this with plain old xibs. You can then specify the name of the nib when you initWithNibNamed:bundle: the view controller.

However, I think it's worth taking a step back. Is it really a good idea to do this? Subclassing view controllers can get messy. View controllers are intended to be self contained units of functionality, which is at odds with subclassing. Unless a view controller is specifically designed to be subclassed then I would avoid doing so. I would suggest that you merge the two view controllers and override setEditing:animated:.

Benedict Cohen
  • 11,592
  • 7
  • 52
  • 65
  • I thought subclassing might be a nice way to implement all the logic we need in the super class and then just adapt the bits we need for editing. This wouldn't be to much of a change. After reading your answer I thought about it again and made the original view controller just a little bit more generic to support both adding and editing. Thanks for your hint! – flohei Jul 23 '13 at 09:58
  • I disagree with your statement "View controllers are intended to be self contained units of functionality". The very structure of View Controllers are subclassed all over the place. Is not a UITableViewController a subclass of UIViewController? Also, in the CS193P course on iTunes U, the instructor specifically created a subclass of UITableViewController that includes a Fetched Results Controller and tells the students to subclass that for their assignments. – DataJock Aug 24 '13 at 01:43
  • @DataJock Did you read my whole answer? The next sentence says "Unless a view controller is specifically designed to be subclassed then I would avoid doing so." `UITableViewController` is specifically designed to be subclassed - in fact it's of little use unless it's subclassed. – Benedict Cohen Aug 27 '13 at 12:22
  • @Benedict Cohen I did read the whole answer, but the way it's written implies that it must be designed that way from Apple and that you shouldn't design them to be subclassed on your own. Which apparently is not how you intended it to sound and I only disagreed with that part of the statement. Sorry if it sounded like I disagreed with everything you said. – DataJock Aug 31 '13 at 00:14
  • Just to say that subclassing your own viewController subclasses is an excellent way to add functionality to them and shouldn't be avoided IMO. The main problem here is a restriction of Storyboards, and one that is particularly annoying. – theLastNightTrain Jun 26 '14 at 15:21