开发者

How to use generic (NSObject) controller with subviews of a UIViewController?

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'v开发者_开发知识库e read have quite hit the spot yet.


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.


It sounds like what you want is what I've coined "reusable UIView widgets" -- reusable widgets that do something / present a display that you can incorporate in your app screens wherever and how many times as you want -- you can create them purely in code or instantiate them by placing their frame in another xib file (but you don't get to change the widgets' internal parameters in the xib file, that would require an IB plugin).

This is an organization that I've never seen discussed anywhere. Part of my frustration early on with iOS programming is wanting something like this but not seeing any way to express it in any example.

See my answer in this question to see how it can be structured:

UIView and initWithFrame and a NIB file. How can i get the NIB file loaded?

I recommend placing all widget-internal handling of direct/low-level events in the uiview widget subclass, and implememnt a delegate protocol for a highler level interaction with the widget client (i.e., "loginRequested:userName:passWord" instead of manually accessing button and textfields internal to the widget).

The (optional but recommended) xib file for the widget has an owner of the widget, and the init code in the widget owns the duty of loading the xib file. The customer of the widget simply instantiates the widget and implements whicever widget delegate functions makes sense for it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜