Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa NSMenuItem的NSView子类_Cocoa_Nsview - Fatal编程技术网

Cocoa NSMenuItem的NSView子类

Cocoa NSMenuItem的NSView子类,cocoa,nsview,Cocoa,Nsview,我想创建NSView的子视图并将其链接到MenuView.xib 我要: - MenuView.m - MenuView.h - MenuView.xib 在xcode中,我创建了我的xib,并将我的“MenuView”设置为customclass。现在,我想通过如下命令以编程方式添加新视图: NSView *vv = [[MenuView alloc] initWithFrame:CGRectMake(0, 0, 300, 200)]; NSMenuItem *newItem = [[NS

我想创建NSView的子视图并将其链接到MenuView.xib

我要:

- MenuView.m
- MenuView.h
- MenuView.xib
在xcode中,我创建了我的xib,并将我的“MenuView”设置为customclass。现在,我想通过如下命令以编程方式添加新视图:

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文件进行渲染?这不对吗

谢谢。

要从nib加载视图(改编自我的):


如果希望文件所有者不是
self
,请将参数更改为
实例化enibwithowner:

使用
NSViewController
,它专为从nib文件加载视图而设计。在nib文件中,将
NSViewController
设置为文件所有者的类,然后设置文件所有者的
视图
出口,使其指向nib中的视图

然后你可以这样做:

NSViewController* viewController = [[NSViewController alloc] initWithNibName:@"YourNibName" bundle:nil];
YourCustomView* view = [viewController view]; //this loads the nib
[viewController release];
//do something with view
NSViewController* viewController = [[NSViewController alloc] initWithNibName:@"YourNibName" bundle:nil];
YourCustomView* view = [viewController view]; //this loads the nib
[viewController release];
//do something with view