与另一个笔尖中 NSArrayController 的选择绑定
我有两个笔尖:
- Store.nib
- Product.nib
Product.nib 的文件所有者是 NSViewController
的子类,它具有一个属性 product
,各种控件绑定到该属性:
@property(nonatomic, retain) SRProduct *product;
Store.nib 有一个 NSArrayController
对象,该对象已绑定到以下属性SRApplicationController
,即以下属性:
@property(nonatomic, retain) NSArray *products;
SRApplicationController
具有该 NSArrayController
对象的出口。
在 -[SRApplicationController init]
方法中,我初始化了一个 SRProductController
对象与 Product.nib nib。在 -[SRApplicationController awakeFromNib]
中,我将产品控制器的视图添加到 Store.nib 中的视图,并绑定 productsArrayController
属性(出口) >SRApplicationController 对象到产品控制器的 product
:
- (id)init {
if (self = [super init]) {
self.productController = [[SRProductController alloc] initWithNibName:@"Product" bundle:nil];
}
return self;
}
- (void)awakeFromNib {
[self.productView removeAllSubviews]; // this method is from a category
[self.productView addSubview:self.productController.view];
[self.productController.view setFrame:self.productView.bounds];
[self.productsArrayController bind:@"selectedObjects" toObject:self.productController withKeyPath:@"product" options:nil];
}
当我运行应用程序时,没有错误,没有警告,控制台保持为空,表格视图包含商店中的所有产品。笔尖显示所有产品,我可以选择它们。问题是 Product.nib 中的所有字段都是空的,但它们绑定到文件所有者的product
属性。谁能帮我解决这个问题吗?提前致谢。 :)
I have two nibs:
- Store.nib
- Product.nib
Product.nib's File owner is a subclass of NSViewController
which has a property product
to which various controls are bound:
@property(nonatomic, retain) SRProduct *product;
Store.nib has an NSArrayController
object which has been bound to a property of SRApplicationController
, which is this property:
@property(nonatomic, retain) NSArray *products;
SRApplicationController
has an outlet to that NSArrayController
object.
In the -[SRApplicationController init]
method I init an SRProductController
object with the Product.nib nib. In -[SRApplicationController awakeFromNib]
I add the view of the product controller to a view in Store.nib, and I bind the productsArrayController
property (the outlet) of the SRApplicationController
object to the product
of the product controller:
- (id)init {
if (self = [super init]) {
self.productController = [[SRProductController alloc] initWithNibName:@"Product" bundle:nil];
}
return self;
}
- (void)awakeFromNib {
[self.productView removeAllSubviews]; // this method is from a category
[self.productView addSubview:self.productController.view];
[self.productController.view setFrame:self.productView.bounds];
[self.productsArrayController bind:@"selectedObjects" toObject:self.productController withKeyPath:@"product" options:nil];
}
When I run the app, I get no errors, no warnings, the console remains empty, the table view with all products in Store.nib shows all products and I can select them. The problem is that all fields in Product.nib are empty, but they are bound to the product
property of the file owner. Can anyone help me with this problem? Thanks in advance. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
某处有一些示例代码展示了如何执行此操作,我不记得是苹果代码还是其他地方的代码。基本上,您需要做的是在每个 nib 文件中都有一个数组控制器。列表样式笔尖中的数组控制器应该正常绑定,并且它的数组控制器应该是可访问的属性。在第二个 nib 文件中,您需要照常绑定数组控制器的内容。您还需要确保此详细信息笔尖的文件所有者与列表笔尖的文件所有者有连接。然后,您将详细信息数组控制器的排序描述符绑定到listController.arrayController.sortDescriptors(实际上可能是我一时想不起来的sortDescriptor)。您还可以以相同的方式绑定选择索引。这将允许详细信息笔尖中的数组控制器跟上列表笔尖中发生的情况,之后您只需照常绑定每个详细信息元素(即产品名称文本字段将其值绑定
arrayController.选择.产品名称
。如果您忘记将详细信息笔尖的数组控制器的排序描述符绑定到列表笔尖中的对应项,每次列表中的选择更改时,详细信息笔尖都会更新,但它可能不会更改为正确的产品(绑定只是传递SelectionIndex 不是选择了什么对象)。
Somewhere there is some sample code that shows how to do this, I can't remember if is Apple code or from somewhere else. Basically what you need to do is have an array controller in each nib file. The array controller in the list style nib should be bound normally and it's array controller should be an accesible property. In the second nib file you need to bind the array controller's content as normal. You also need to make sure that the file's owner of this detail nib has a connection to the file's owner of the list nib. You then bind the sort descriptor for the detail array controller to
listController.arrayController.sortDescriptors
(it might actually besortDescriptor
can't remember off the top of my head). You also bind the selection index in the same manner. This will allow the array controller in the detail nib to keep up with what is going on in the list nib, after that you just bind each detail element as normal (i.e. the product name text field would have it's value boundarrayController.selection.productName
.If you forget to bind the sort descriptor of the detail nib's array controller to it's counterpart in the list nib the detail nib will update each time the selection changes in the list, but it might not change to the proper product (the binding just passes the selectionIndex not what object is selected).
为 Product.nib 分配视图控制器时,您应该将其“product”属性绑定到数组控制器的选择,它只能在代码中完成,但这将避免需要数组控制器的多个实例,并避免需要将它们绑在一起,使它们看起来一样。
另外,我建议不要将数组控制器的内容绑定到您自己的 NSArray,如果您不绑定该属性,数组控制器将分配和管理自己的数组。您将能够直接添加/删除对象,而不必依赖您自己的属性来仔细通知 NSArrayController 发生了更改。
“内容”绑定允许将数组控制器的排列对象绑定到另一个控制器的内容,以便能够以不同的方式对内容进行过滤和排序。
When allocating the view controller for the Product.nib you should bind its "product" property to your array controller's selection, it can only be done in code, but that will avoid the need for multiple instances of an array controller, and avoid the need to bind them together so they look the same.
Also, I suggest not to bind the array controller's content to your own NSArray, if you do not bind that property the array controller will allocate and manage its own array. You'll be able to add/remove objects from it directly instead of having to rely on your own property to carefully notify the NSArrayController that a change occurred.
The "content" binding is there to allow to bind an array controller's arrangedObjects to the content of another controller to be able to filter and sort the content differently.