views:

364

answers:

2

I have a UIViewController that is loading several subviews at different times based on user interaction. I originally built all of these subviews in code, with no nib files. Now I am moving to nib files with custom UIView subclasses.

Some of these subviews display static data, and I am using loadNibNamed:owner:options: to load them into the view controller. Others contain controls that I need to access.

I (sort of) understand the reasons Apple says to use one view controller per screen of content, using generic controller objects (NSObjects) to manage subsections of a screen.

So I need a view controller, a generic controller, a view class and a nib. How do I put this all together?

My working assumptions and subsequent questions:

  • I will associate the view class with the nib in the 'class identity' drop down in IB.
  • The view controller will coordinate overall screen interactions. When necessary, it will create an instance of the generic controller.
  • Does the generic controller load the nib? How?
  • Do I define the outlets and actions in that view class, or should they be in the generic controller?
  • How do I pass messages between the view controller and the generic controller?

If anyone can point me to some sample code using a controller in this way, it will go a long way to helping me understand. None of the books or stackoverflow posts I've read have quite hit the spot yet.

A: 

Okay, I think I figured it out:

  1. Extend NSObject to make your CustomController
  2. Define your outlets & actons in CustomController.h, including a reference to the UIView in your nib
  3. Set the File's Owner of your nib to CustomController
  4. Hook up all your outlets & actions as usual, including the UIView outlet
  5. In your CustomController.m init, load the nib

- (id)init {
    self = [super init];
    if (self != nil)
        [self loadNib];

    return self;
}

- (BOOL)loadNib {
    NSArray *topLevelObjs = nil;
    topLevelObjs = [[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:self options:nil];

    if (topLevelObjs == nil) {
        NSLog(@"Error! Could not load nib file.\n");
        return NO;
    }
    return YES;
}

The new NSObject based controller will work very much like a view controller.

wanderlust
A: 

I did not get it completely, can you put the sample application for the CustomController?

Niraj