Interface Builder: How to load view from nib file

前端 未结 4 1880
日久生厌
日久生厌 2021-02-10 19:20

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

4条回答
  •  名媛妹妹
    2021-02-10 19:56

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

    #import 
    
    @interface NewViewController : NSViewController
    
    @end
    
    #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;
    }
    
    @end
    

    Your xib name is yourview.xib

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

提交回复
热议问题