简体   繁体   中英

Subclassing a main view controller with outlets

I would like to create a main view controller in order to handle a custom navigation bar and its behavior.

Then, I would like to subclass this new class to use its behavior and link a "contentView" IBOutlet for each of my ViewControllers.

Here is my BaseViewController.xib: 在此处输入图片说明

TestViewController.xib: linking the "contentView" outlet for its parent 在此处输入图片说明

TestViewController.h: inherit from BaseViewController

#import "BaseViewController.h"

@interface TestViewController : BaseViewController

@end

TestViewController.m: should use outlets from TestViewController.xib

- (id)init
{
    self = [super init];
    if (self) {
        [[NSBundle mainBundle] loadNibNamed:@"TestViewController"
                                      owner:self
                                    options:nil];
    }

    return self;
}

Obviously, I would like to have the green bar from MainViewController with the white view and the label from TestViewController, but it's actually not working, here is the result: 在此处输入图片说明

If in TestViewController.xib, I link the "view" outlet with a view, it's actually overriding this screen and I don't have the green bar from MainViewController, so I guess that my import is working, but I can't understand why it's not working with the contentView.

Any idea ?

Cheers!

Cyril

You will have to decide which controller should control the view with the content.

  • Either you have the base view controller control it - in which case you do not need to subclass it. Just add a subclass of UIView as a subview and do all the logic there. The view can also have its own nib file.
  • Or you subclass your base view controller - but then you cannot have a separate view controller nib. You can do all the view controller logic (data sources etc) in the subclass (which is also a view controller), but it will use the nib of the base view controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM