在Mac开发中会遇到需要加载Xib文件的时候,过程与iOS加载Xib文件大同小异。下面说一下方法:
新建一个工程“LoadXib”,然后新建一个Xib文件,然后拉一些控件进去,如图1:
图1
这个MyXib.xib就是我们要加载的Xib,加载代码:
- (IBAction)jumpToLogIN:(id)sender {
[_jumpToLoginButton setHidden:YES];
NSView *view = nil;
NSNib *xib = [[NSNib alloc] initWithNibNamed:@"MyXib" bundle:nil];
NSArray *viewsArray = [[NSArray alloc] init];
[xib instantiateWithOwner:nil topLevelObjects:&viewsArray];
for (int i = 0; i < viewsArray.count; i++) {
if ([viewsArray[i] isKindOfClass:[NSView class]]) {
view = (NSView *)viewsArray[i];
break;
}
}
//view.wantsLayer = YES;
//view.layer.backgroundColor = [NSColor redColor].CGColor;
[self.view addSubview:view];
}
运行效果: