Skip to content
This repository has been archived by the owner on Mar 24, 2022. It is now read-only.

Nib loading helpers

Brian Hardy edited this page Jun 1, 2017 · 7 revisions

UIKit+PivotalCore adds categories on UIView and UIViewController which aim to make it easier to load nib-based classes from other nibs.

To use this functionality, a few steps are required:

Loading nib-based Views

For views which have a nib as well as an implementation (for instance, with outlets or actions bound back to the instance)

  1. Lay out your containing view's nib, and place a placeholder view for the class you want to instantiate
  2. Edit the view's class to be the class you want to load
  3. Ensure the class you want to load has a nib with the same name as the class (e.g. FooClass.{h,m} has a corresponding nib FooClass.xib, FooClass~iphone.xib, or FooClass~ipad.xib)
  4. Set it up with autolayout constraints if appropriate
  5. Give the view a unique restoration identifier beginning with "placeholder" (e.g. "placeholder-FooClass")

When this nib is loaded, the view will be correctly instantiated with its inner bindings and constraints set up, preserving its outer bindings and constraints. This aims to make it easier to reuse view classes with nibs as well as implementation files in larger projects.

For example, say you have a view class ReusedView:

Reused View Files

ReusedView has a ReusedView.xib with some layout and bindings that you want to re-use:

Reused View Nib

To load ReusedView.xib from within MainController's xib you set up MainController's xib as follows:

Containing Nib

The placeholder view is replaced by the view specified by ReusedView.xib. The binding of the ReusedView instance to the MainController's reusedView property declared in MainController.xib is maintained with the replacing instance:

After

Loading nib-based ViewControllers

It is also possible to load a child view controller's nib-based view from a parent view controller's nib. In order to do this you need to:

  1. Add a child view controller to the parent view controller's nib
  2. Add a placeholder view in the parent view controller's nib
  3. Assign the placeholder view to the child controller's view outlet
  4. Give the placeholder view a unique restoration identifier beginning with "placeholder"
  5. Add the following lines of code in the parent view controller's viewDidLoad
[self addChildViewController:self.childViewController];
[self.childViewController didMoveToParentViewController:self];

For example, with ChildViewController's nib that looks like this:

Child View Controller

Set up the containing view controller with a nib like this (where the child view controller's class is set to ChildViewController):

Containing View Controller

The placeholder view bound as ChildViewController's view in MainController's nib will be replaced by the view specified in ChildViewController.xib. All of the bindings in ChildViewController.xib and all of the layout specified in MainController's nib will be maintained:

Containing View Controller with Inserted Child

Clone this wiki locally