向工具栏添加自定义视图 [英] Adding a custom view to toolbar
问题描述
我与Cocoa一起努力了2个小时,现在没有成功。我想添加一个自定义视图到工具栏。所以,我添加了一个NSToolbar到窗口(与IB),并添加了我的视图(它的工作完美)。 IB自动创建一个NSToolbarItem。
我按照Apple的说明进行操作: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Toolbars/Tasks/AddRemoveToolbarItems.html#//apple_ref/doc/uid/20000755-BBCGJCDJ
I'm struggling with Cocoa for 2 hours now without success. I want to add a custom view to the toolbar. So, I added a NSToolbar to the window (with IB), and added my view (which works perfectly). IB automatically created a NSToolbarItem. I followed the instructions from Apple here: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Toolbars/Tasks/AddRemoveToolbarItems.html#//apple_ref/doc/uid/20000755-BBCGJCDJ
问题是,我现在不知道该怎么做,视图不显示,虽然它的标签显示在窗口中。
The problem is that I don't know what to do now, the view doesn't show although it's label is displayed in the window.
这里是我绘制的代码(非常简单,它用于测试目的)
Here's the code I use to draw (very simple, it's for testing purpose)
- (void)drawRect:(NSRect)dirtyRect {
[[NSColor blackColor] set];
[[NSBezierPath bezierPathWithRect:self.bounds] fill];
}
有人可以帮助我吗?
提前感谢。
推荐答案
我解决了这个问题。
在笔尖的根部。我添加了一个经典的NSToolbarItem并创建了两个插座:一个用于自定义视图,一个用于NSToolbarItem。在 - (void)awakeFromNib
,我在自定义视图的NSToolbarItem上调用 setView:
。
I put my custom view in the root of the nib. I added a classic NSToolbarItem and created two outlets: one for the custom view and one for the NSToolbarItem. On -(void)awakeFromNib
, I called setView:
on the NSToolbarItem with the custom view.
根据互联网上的一些资源,这是一个与Interface Builder的错误。
According to some ressources on the internet, it is a bug with Interface Builder.
这篇关于向工具栏添加自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!