NSViewController 和 Nib 中的多个子视图
我很难理解如何使用 Interface Builder 和 NSViewController 加载视图。
我的目标是拥有一个满足以下描述的视图:顶部的顶栏(类似于工具栏,但不完全一样)跨越视图的整个宽度,下面是第二个“内容视图”。这个复合视图由我的 NSViewController 子类拥有。
为此使用 Interface Builder 是有意义的。我创建了一个视图笔尖,并向其中添加了两个子视图,并正确放置了它们(带有顶部栏和内容视图)。我已将File's Owner
设置为MyViewController
,并连接了插座等。
我希望加载的视图(栏和内容)也在它们自己的笔尖中(这可能是让我绊倒的原因),并且这些笔尖将其自定义类设置为相应的 NSView 子类(如果适用)。我不确定将什么设置为他们的File's Owner
(我猜测MyController
,因为它应该是他们的所有者)。
唉,当我初始化 MyViewController
的实例时,我的笔尖实际上都没有显示。我已经将它正确添加到我的 Window 的 contentView 中(我已经检查过其他情况),实际上,事情已经加载了。也就是说,awakeFromNib
被发送到栏视图,但它不会显示在窗口中。我想我肯定在某个地方交叉了一些电线。也许有人可以伸出援手来缓解我的一些挫败感?
编辑一些代码来显示我正在做的事情
当我的应用程序完成启动时,从应用程序委托加载控制器:
MyController *controller = [[MyController alloc] initWithNibName:@"MyController" bundle:nil];
[window setContentView:[controller view]];
然后在我的 initWithNibName 中,除了暂时调用 super 之外,我什么也不做。
I'm having a difficult time wrapping my head around loading views with Interface Builder and NSViewController.
My goal is to have a view which meets the following description: Top bar at the top (like a toolbar but not exactly) which spans the entire width of the view, and a second "content view" below. This composite view is owned by my NSViewController
subclass.
It made sense to use Interface Builder for this. I have created a view nib, and added to it two subviews, laid them out properly (with the top bar and the content view). I've set File's Owner
to be MyViewController
, and connected outlets and such.
The views I wish to load in (the bar and the content) are also in their own nibs (this might be what's tripping me up) and those nibs have their Custom Class set to the respective NSView subclass where applicable. I'm not sure what to set as their File's Owner
(I'm guessing MyController
as it should be their owner).
Alas, when I init an instance of MyViewController
none of my nibs actually display. I've added it to my Window's contentView properly (I've checked otherwise), and actually, things sort of load. That is, awakeFromNib
gets sent to the bar view, but it does not display in the window. I think I've definitely got some wires crossed somewhere. Perhaps someone could lend a hand to relieve some of my frustration?
EDIT some code to show what I'm doing
The controller is loaded when my application finishes launching, from the app delegate:
MyController *controller = [[MyController alloc] initWithNibName:@"MyController" bundle:nil];
[window setContentView:[controller view]];
And then in my initWithNibName I don't do anything but call to super for now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将每个视图分解为自己的笔尖并使用 NSViewController 时,典型的处理方法是为每个笔尖创建一个 NSViewController 子类。然后,每个相应 nib 文件的文件所有者将被设置为该
NSViewController
子类,并且您将view
出口连接到 nib 中的自定义视图。然后,在控制主窗口内容视图的视图控制器中,实例化每个 NSViewController 子类的实例,然后将该控制器的视图添加到您的窗口中。一段快速的代码 - 在此代码中,我调用主要内容视图控制器
MainViewController
,“工具栏”的控制器是TopViewController
,其余部分内容是ContentViewControllerWhen breaking out each view into its own nib and using
NSViewController
, the typical way of handling things is to create anNSViewController
subclass for each of your nibs. The File's Owner for each respective nib file would then be set to thatNSViewController
subclass, and you would hook up theview
outlet to your custom view in the nib. Then, in the view controller that controls the main window content view, you instantiate an instance of eachNSViewController
subclass, then add that controller's view to your window.A quick bit of code - in this code, I'm calling the main content view controller
MainViewController
, the controller for the "toolbar" isTopViewController
, and the rest of the content isContentViewController
MainViewController 不应该是 NSWindowController 的子类吗?类中的插座连接到 MainMenu.xib 中主窗口中的视图元素?
让我们希望旧的线程仍然被阅读......
Should not MainViewController be a subclass of NSWindowController? And the outlets in the class connected to view elements in the main Window in MainMenu.xib?
Let's hope old threads are still read...