dhrm dhrm - 1 month ago 11x
Objective-C Question

Interface Builder: How to load view from nib file

I have a MyCustomView subclassed from NSView designed in a .xib.

I would like to insert this view into some of my other xib's round my application. How should I do this? If i drag a custom view and change the class to MyCustomView, but that does not load my xib-file. Can this only be done programmatically or is there a way to do this inside interface builder?


Here is a very small demo-project:

It contains the default MainMenu xib and my CustomView xib. I would like my CustomView.xib to be displayed inside the custom view added to my MainMenu.xib -- using as less code as possible.


For loading the view you need to add on your window:- Created custom class of view inheriting to NSViewController

#import <Cocoa/Cocoa.h>

@interface NewViewController : NSViewController


#import "NewViewController.h"

@implementation NewViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Initialization code here.

    return self;


Your xib name is yourview.xib

- (void)windowDidLoad {
    NSViewController *yourVC = [[NewViewController alloc] initWithNibName:@"NewViewController" bundle:nil];
    [[[self window] contentView] addSubview:[yourVC view]];