如何显示从单独的 NIB 文件加载的子视图
我正在开发一个 Cocoa 桌面应用程序,它使用 iTunes 风格的源列表:源列表中的不同元素会导致主要内容区域显示不同的视图。
内容区域完全由(垂直)NSSplitView
填充;左侧是 NSOutlineView 源列表。当用户选择左侧的项目时,相关视图将出现在拆分器的右侧。
我可以通过将所有内容放入一个 NIB 文件中并将无边框的 NSTabView
放在拆分器的右侧,使其工作得足够好;要切换视图,我只需更改所选选项卡即可。但是将所有视图放在一个 NIB 中是一种不好的做法,因此我尝试将每个子视图移动到它们自己的 NIB 文件中。
我对这个过程的大部分内容都有一个很好的了解 - 我为每个视图创建了一个 NSViewController 子类(EntityDetailViewController
、GroupDetailViewController
、等等),将每个新 NIB 的文件所有者设置为相关的控制器类,在每个 NIB 中设置 view
连接,并重新设计绑定。我不知道如何实际更改屏幕上显示的子视图。
我尝试在右侧使用默认的通用 NSView
并向其发送 addSubview:
消息;我尝试将其作为第一个子视图连接并调用
NSView *newSubview = /* get subview from the new subview controller */
[[subview superview] replaceSubview:subview with:newSubview];
[self setSubview:newSubview];
但一切都将空格留空。如何显示从单独的 NIB 加载的子视图?
I'm developing a Cocoa desktop application that uses a source list in the style of iTunes: different elements in the source list cause the main content area to display different views.
The content area is entirely filled with a (vertical) NSSplitView
; on the left is an NSOutlineView
source list. When the user selects an item on the left, the relevant view appears on the right side of the splitter.
I can make it work well enough by putting everything in one NIB file and putting a borderless NSTabView
to the right of the splitter; to switch views, I just have to change the selected tab. But putting all the views in one NIB is bad practice, so I'm trying to move each of the subviews into their own NIB files.
I have a pretty good idea of most of this process — I've created an NSViewController
subclass for each of these views (EntityDetailViewController
, GroupDetailViewController
, and so on), set the File's Owner of each new NIB to the relevant controller class, set the view
connection in each NIB, and reworked the bindings. What I don't know is how to actually change which subview is being shown on screen.
I've tried using the default generic NSView
on the right side and sending it addSubview:
messages; I've tried connecting to it as the first subview and calling
NSView *newSubview = /* get subview from the new subview controller */
[[subview superview] replaceSubview:subview with:newSubview];
[self setSubview:newSubview];
But everything just leaves the space blank. How do I display a subview loaded from a separate NIB?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的总体方法似乎是正确的。您是否已验证加载的视图控制器中的
newSubview
是否为非零?newSubview
的框架在添加到视图层次结构之前和之后看起来是否正确?最后,您在什么时候使用replaceSubview:
代码?它应该在awakeFromNib
或更高版本中。Your general approach seems sound. Have you verified that the
newSubview
from the loaded view controller is non-nil? Does the frame ofnewSubview
look correct before and after it is added to the view hierarchy? Lastly, at what point are you using thereplaceSubview:
code? It should be inawakeFromNib
or later.