从XIB加载NSView子类 [英] Loading NSView subclass from a XIB

查看:563
本文介绍了从XIB加载NSView子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在NSMenuItem中使用自定义视图。我已经创建了一个新的视图XIB并在其中自定义了视图。如何使用setView:方法加载该视图并在NSMenuItem中设置它?

I need to use a custom view into a NSMenuItem. I've created a new view XIB and customized the view in it. How can I load that view and set it in the NSMenuItem using the setView: method?

更新:我找到了一个解决方案,但现在鼠标悬停时,自定义视图的菜单项不会突出显示。何我能解决这个问题吗?

UPDATE: I've found a solution but now the menu item with the custom view doesn't highlight on mouse over. Ho can I solve this problem?

推荐答案

我创建了一个NSViewController子类,并将其设置为XIB中文件的所有者。然后我将文件所有者的视图出口设置为XIB中的视图,最后在需要时我实例化了视图控制器,并使用以下代码将其视图设置为NSMenuItem视图:

I've created a NSViewController subclass and set it as the file's owner in the XIB. Then I've set the view outlet of the file's owner to the view in the XIB and finally when needed I've instantiated the view controller and set it's view as the NSMenuItem view with the following code:

CustomViewController *viewController = [[CustomViewController alloc] initWithNibName:@"NibName" bundle:nil];

NSMenuItem *menuItem = [[NSMenuItem alloc] init];
[menuItem setView:[viewController view]];

这篇关于从XIB加载NSView子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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