如何将 nib 自定义视图绑定到 NSVIew 子类
我有一个简单的要求。 单击 + 按钮时,我尝试将自定义视图添加到 SplitView
中。
我创建了一个 MyCustomView 类,它是 NSView 的子类。
在应用程序 nib 文件中,我有一个包含按钮等的自定义视图。
现在如何每次分配一个新的 MyCustomView ?
有一个例子可以做到这一点吗?
我希望像
MyCustomView *v1 = [[MyCustomView alloc] init];
..
..
[splitView addSubView:v1];
[splitView addSubView:v2];
...
请帮忙
I have a simple requirement.
On Click of a + button, I am trying to add a custom view to a SplitView
.
I have created a class MyCustomView which is a subclass of NSView
In the applications nib file, I have a custom view which contains the buttons etc.
Now How to allocate a new MyCustomView every time ?
Is there an example to do this?
I am hoping something like
MyCustomView *v1 = [[MyCustomView alloc] init];
..
..
[splitView addSubView:v1];
[splitView addSubView:v2];
...
Please help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您的描述很难准确判断您所描述的内容,但让我们看看我是否理解您的意思。您希望每次单击“+”时将自定义视图组件的“副本”添加到拆分视图中,对吧?
执行此操作的绝对最佳方法是将要复制的自定义视图程序集(“原型”)放入其自己的 xib 中。对于您想要表示的每个对象,您将从 xib 实例化一个新副本并将其交给某个所有者,然后将其添加到某个父视图(在您的情况下是分割视图......对于无限数量的视图来说很奇怪,但我没有足够的细节来说明其他情况)。
所以。在现代 Cocoa 世界中,这样的视图程序集可能应该有自己的视图控制器(NSViewController)。这使事情变得更容易,因为 xib 的文件所有者将是 MyCustomViewController 的实例,其 -view 连接到 xib 中的主容器视图(您的自定义视图及其所有子视图),并且其 -representedObject 设置为任何模型您的自定义视图代表的对象。然后,您的应用程序将维护模型对象的所有视图控制器的列表(可能是数组或字典)。有关如何从 nibs/xibs 加载的详细信息,请参阅这个问题/答案。
这基本上就是 NSCollectionView 有效(尽管视图必须全部具有相同的大小 - 可能不适合您)。在这种情况下,集合视图对应于您的拆分视图; NSCollectionViewItem 对应于你的 MyCustomViewController (事实上在 10.5 及以上版本 NSCollectionViewItem 是 NSViewController 的子类);您的自定义视图是集合视图项的主视图。对于其集合中的每个模型对象,它实例化一个 NSCollectionViewItem 并从 xib 加载视图原型(理想情况下,但这是可选的),并使用它来设置项目的视图,然后设置项目的表示对象(模型对象) 。
我希望这能澄清一些事情。为了理解足够的具体细节,您需要阅读一些内容,但如果您仍然遇到困难,您可以尝试编辑您的问题以澄清或提出一个新的、更具体的问题。
It's hard to tell exactly what you're describing based on your description but let's see if I understand you. You want to add a "copy" of your custom view assembly into a split view each time "+" is clicked, right?
The absolute best way to do this is to put the custom view assembly that will be copied (the "prototype") in its own xib. For each object you want to represent, you will instantiate a new copy from the xib and give it to some owner then add it to some parent view (a split view in your case ... odd for an unlimited number of views, but I don't have enough detail to say otherwise).
So. In the modern Cocoa world, such a view assembly should likely have its own view controller (NSViewController). This makes things easier for you since the xib's File's Owner will be an instance of your MyCustomViewController, whose -view is connected to the main container view in the xib (your custom view with all its subviews) and whose -representedObject is set to whatever model object your custom view represents. Your app will then maintain a list (an array or a dictionary, perhaps) of all the view controllers for the model objects. See this SO question/answer for a run-down of how to load from nibs/xibs.
This is basically how an NSCollectionView works (though the views must all be the same size - might not work for you). The collection view corresponds to your split view in this case; NSCollectionViewItem corresponds to your MyCustomViewController (and in fact on 10.5 and above NSCollectionViewItem is a subclass of NSViewController); your custom view is the collection view item's main -view. For each model object in its collection, it instantiates an NSCollectionViewItem and loads the view prototype from a xib (ideally, but this is optional), and uses this to set the item's view, then it sets the item's represented object (the model object).
I hope this clarifies things a bit. You've got some reading to do in order to understand enough of the nuts and bolts, but if you're still stuck, you might try editing your question to clarify or opening a new, more specific question.