NSMenuItem 的 Cocoa 自定义视图
我正在开发一个小应用程序,它在 NSStatusBar 的菜单中显示活动安装。到目前为止,它看起来像这样:
我想在每个菜单的右侧添加一个弹出按钮项目(如 Finder 中的左侧栏)。我知道我必须创建一个自定义视图并使用 setView: 方法设置它。问题是我对 Cocoa 非常陌生,现在我不知道如何深入了解自定义视图主题。事实上,我编写了很多程序,但到目前为止从未使用过接口:)。你们中有人有向 NSMenuItem 添加自定义视图的好教程吗?
我认为这个应用程序会非常方便,因为您可以隐藏桌面上安装的图标。问题是您总是必须返回查找器才能卸载卷...
感谢您提供的任何帮助或提示!
I am developing a small app that display the active mounts in a menu in the NSStatusBar. So far it looks like this:
I want to add an eject button to the right of each menu item (like the left bar in the Finder). I know that I have to create a custom view and set it with the setView: method. The problem is that I am very new to Cocoa and right now I don't know how to dive into the custom view topic. Actually, I programmed a lot but never worked with interfaces so far :). Does anyone of you have a good tutorial for adding a custom view to a NSMenuItem?
I think this app will be very handy because you can hide the mounted icons from your desktop. The problem is that you always have to go back to the finder to unmount a volume...
Thanks for any help or tips you have!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它不必是自定义视图。它可以只是一个标准 NSView,充当标准控件的容器。
对于新手来说,棘手的部分是使视图可重用。您将需要自己的 NSViewController 子类以及相应的视图 nib/xib (使用您的名称标签和弹出按钮进行设置)。对于每个菜单项,您将使用 XIB ( -initWithNibName:bundle: ) 实例化一个新的 NSViewController 并将其表示的对象设置为您的安装点。您的视图控制器子类将拥有响应弹出按钮单击、将标签设置为其表示的对象的路径等所需的一切。
It doesn't have to be a custom view. It can just be a standard NSView that acts as a container for standard controls.
The tricky part for a newbie is making the view reusable. You'll want your own NSViewController subclass with a corresponding view nib/xib (set up with your name label and eject button). For each menu item, you'll instantiate a new NSViewController with the XIB ( -initWithNibName:bundle: ) and set its represented object to your mount point. Your view controller subclass will have all it needs to respond to the eject button click, set the label to its represented object's path, etc.