0

Is it possible to have a ViewController use one nib, while my ViewController's subclass points to another?

What I am trying to do:

I have an application that has a main menu that will take you to any section. Within the section you are in, you can pull down a different sub-section menu, which will take you to different pages.

I would like to have all the sub-section menu stuff be handled by a base ViewController class that each sub-section page would inherit from. That way I only need to write code to handle showing the sub-section menu in the base class, and all sub-section pages will automatically have it.

Though I don't know if it is possible to instantiate a ViewController with one nib, and tell it's base class to hookup to a different one.

Any help or tips appreciated :)

Thank you.

Jason
  • 5,277
  • 4
  • 17
  • 12

1 Answers1

1

In iPhone-world, a UIViewController or UIViewController subclass is generally responsible for controlling an entire view hierarchy. This obviously breaks down in iPad-world with controllers such as the UISplitViewController.

So, I will make some assumptions about your app set up based on the description you gave above: You might consider having a MainMenuViewController. This could, of course, inherit from some BaseViewController if you need it to, but the nib for the MainMenuViewController would allow you to navigate to the various sections. Perhaps this nib consists of nothing but a bunch of UIButtons. You tap a button and that takes you to a UIViewController subclass that corresponds to that section. For instance, if the section is Sports Teams, maybe that View Controller's nib consists of nothing but a UITableView that lists local sports teams. This View Controller can have a way to access this sub-section menu, which maybe lists details for a specific sports team. This "detail" section would be its own UIViewController subclass (inheriting from a base ViewController as you stated), which has its own nib, perhaps with UILabels listing the name, city, number of players, etc.

If the view hierarchy from one sub-menu to another is the same, you can reuse the view controller and just update the contents of its view hierarchy. If the view hierarchy is drastically different from sub-menu to sub-menu, you may want to consider a different UIViewController subclass that manages each sub-section's view hierarchy.

jmstone617
  • 5,707
  • 2
  • 24
  • 26
  • The view hierarchy from sub-menu to sub-menu varies a lot. Though giving it more thought, I'm wondering if I can just create a standalone nib, and pull the ui elements I need for my sub-section menu through UINib* object. – Jason Apr 22 '12 at 16:29
  • You don't understand how nibs work, really. You're not going to be taking on any kind of noticeable performance penalty by using a nib for each view controller. – jmstone617 Apr 22 '12 at 18:40
  • Noted, and that is exactly what I did. Thanks :) – Jason May 06 '12 at 17:25