martes, 13 de marzo de 2012

Child UITableViewController with cell prototyping on Storyboard

One of the most powerful and code reduction tools in iOS 5 SDK is the Cell Prototyping with Storyboards. You can design directly in Xcode your dynamic cells, and even design the whole table view cells if your table cells are static (as in a grouped style table view to build a menu) and assign outlets to cell subviews to programmatically set its content.

Other new and powerful tool in iOS 5 is View Controller containment, through view controller hierarchy manipulation; the "one screenful of content equals one UIViewController" rule is no longer enforced. Now you can get better code reuse since UIViewController has a list of "child" view controllers, one in charge of controlling one branch of its view hierarchy; UIViewController can transfer the control of one view to another view controller.

But, what if you try to mix both technologies? Storyboarding does not have a direct way to define child view controllers. Some people (as in here) are doing it the wrong way: add an "Object" to the scene, change its class and add some code. In some situations it may work, but it's not the way to go: try to do it with a child UITableViewController with static cells.

How to do it then?

First of all: create a new scene in your storyboard with your UITableViewController subclass and add your static cells as usual. Do not connect this newly created scene with any segue, but don't forget to assign the proper value to the identifier field of your view controller

Now, in your parent view controller add some code in its -awakeFromNib and -viewDidLoad method implementation:

    // instantiate and assign the child view controller to a property to have direct reference to it in
    self.propiedadesVC=[self.storyboard instantiateViewControllerWithIdentifier:@"BGRutaPropiedadesListViewController"]; 
    // configure your child view controller
    // add your child view controller to children array
    [self addChildViewController:self.propiedadesVC]; 

    [self.propiedadesVC didMoveToParentViewController:self];

- (void)viewDidLoad
    [super viewDidLoad];
    // configure chld view controller view's frame
    self.propiedadesVC.view.frame=CGRectMake(640, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height);
    // add child's view to view hierarchy
    [self.scrollView addSubview:self.propiedadesVC.view];

In this sample, the view is added as a subview of a paging scroll view. In case you need to scroll through n views you can instantiate any number of child view controllers.

Simple and straightforward. Storyboarding rocks!

2 comentarios:

  1. Where and how to declare this:


    1. propiedadesVC is declared at the class @interface like any other property

      It's the previously mentioned custom UITableViewController subclass.

      In this case, "ruta" is the model object: the table view controller will populate its table view with data from it.