Interface Builder:如何从nib文件加载视图 [英] Interface Builder: How to load view from nib file

查看:144
本文介绍了Interface Builder:如何从nib文件加载视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从.xib中设计的 NSView 子类的MyCustomView。



我想将这个视图插入我的其他xib的轮回我的应用程序。我应该怎么做?如果我拖动自定义视图,并将类更改为MyCustomView,但不会加载我的XIB文件。这只能通过编程方式或有一种方法来做到这一点在界面构建器?



EDIT1:



这是一个非常小的演示项目:
http://s000.tinyupload.com/index.php?file_id=09538344018446482999



它包含默认的MainMenu xib和我的CustomView xib。我想要将 CustomView.xib 显示在添加到我的MainMenu.xib 中的自定义视图中 - 尽可能少的代码。

$ b $为了加载视图,你需要在你的窗口上添加: -
创建的自定义视图类继承到NSViewController

$ b $

b

  #import< Cocoa / Cocoa.h> 

@interface NewViewController:NSViewController

@end

#importNewViewController.h

@implementation NewViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if(self){
//初始化代码在这里。
}

return self;
}

@end

您的xib名称为 yourview.xib

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


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?

EDIT1:

Here is a very small demo-project: http://s000.tinyupload.com/index.php?file_id=09538344018446482999

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

@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]];
}

这篇关于Interface Builder:如何从nib文件加载视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆