我想创建NSView的子视图并将其链接到MenuView.xib。
我会有:
- MenuView.m
- MenuView.h
- MenuView.xib在xcode中,我创建了自己的xib,并将其设置为customclass my 'MenuView‘。现在,我想通过如下命令以编程方式添加我的新视图:
NSView *vv = [[MenuView alloc] initWithFrame:CGRectMake(0, 0, 300, 200)];
NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:@"title" action:nil keyEquivalent:@""];
[newItem setView:vv];但我只看到一个没有任何内容的空白空间。我如何告诉类MenuView.m使用MenuView.xib文件呈现?这是错的吗?
谢谢。
发布于 2011-09-14 12:14:04
使用NSViewController,它是为从nib文件加载视图而设计的。在nib文件中,将NSViewController设置为文件的所有者的类,然后设置文件的所有者的view插座,使其指向nib中的视图。
然后你可以这样做:
NSViewController* viewController = [[NSViewController alloc] initWithNibName:@"YourNibName" bundle:nil];
YourCustomView* view = [viewController view]; //this loads the nib
[viewController release];
//do something with view发布于 2011-09-14 04:49:46
从nib加载视图(改编自我的another answer ):
NSNib *nib = [[NSNib alloc] initWithNibNamed:@"MenuView" bundle:nil];
NSArray *nibObjects;
if (![nib instantiateNibWithOwner:self topLevelObjects:&nibObjects]) return nil;
NSMenuItem *item = nil;
for (id obj in nibObjects)
if ([obj isKindOfClass:[NSMenuItem class]]) {
item = obj;
break;
}
[someView insertSubview:item];如果您想要self以外的其他用户作为文件所有者,请将参数更改为instantiateNibWithOwner:。
https://stackoverflow.com/questions/7403404
复制相似问题