【问题标题】:Binding with the selection of an NSArrayController in another nib与另一个 nib 中的 NSArrayController 选择绑定
【发布时间】:2011-04-17 18:09:28
【问题描述】:

我有两个笔尖:

  1. Store.nib
  2. Product.nib

Product.nib 的文件所有者是NSViewController 的子类,它有一个属性product,各种控件都绑定到该属性:

@property(nonatomic, retain) SRProduct *product;


Store.nib 有一个NSArrayController 对象,该对象已绑定到SRApplicationController 的属性,即此属性:

@property(nonatomic, retain) NSArray *products;

SRApplicationController 有一个到该NSArrayController 对象的出口。


-[SRApplicationController init] 方法中,我使用 Product.nib nib 初始化一个 SRProductController 对象。在-[SRApplicationController awakeFromNib] 中,我将产品控制器的视图添加到Store.nib 中的视图中,并将SRApplicationController 对象的productsArrayController 属性(插座)绑定到产品控制器的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];
}

当我运行该应用程序时,我没有收到任何错误,没有警告,控制台仍然是空的,Store.nib 中包含所有产品的表格视图显示了所有产品,我可以选择它们。问题是 Product.nib 中的所有字段都是空的,但它们绑定到文件所有者的 product 属性。谁能帮我解决这个问题?提前致谢。 :)

【问题讨论】:

    标签: objective-c cocoa model-view-controller cocoa-bindings


    【解决方案1】:

    某处有一些示例代码显示了如何执行此操作,我不记得是 Apple 代码还是来自其他地方。基本上你需要做的是在每个 nib 文件中都有一个数组控制器。列表样式 nib 中的数组控制器应该是正常绑定的,它的数组控制器应该是一个可访问的属性。在第二个 nib 文件中,您需要像往常一样绑定数组控制器的内容。您还需要确保此详细 nib 的文件所有者与列表 nib 的文件所有者有连接。然后将详细数组控制器的排序描述符绑定到listController.arrayController.sortDescriptors(实际上可能是sortDescriptor,我不记得了)。您还可以以相同的方式绑定选择索引。这将允许细节 nib 中的数组控制器跟上列表 n​​ib 中正在发生的事情,之后您只需像往常一样绑定每个细节元素(即产品名称文本字段将其值绑定arrayController.selection.productName。 如果您忘记将详细 nib 的数组控制器的排序描述符绑定到列表 nib 中的对应项,则每次列表中的选择更改时,详细 nib 都会更新,但它可能不会更改为正确的产品(绑定只是通过selectionIndex 不是选择什么对象)。

    【讨论】:

    • 我得到这个错误,实际上:[<NSArrayController 0x10061cca0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key selection.
    • 您是否记得@synthesize 属性或自己制作访问器?还应检查以确保插座已连接。
    • 所以基本上你希望 NSArrayController 的两个实例共享相同的内容? Product.nib 只需要一个实例,它就是选择。
    【解决方案2】:

    为 Product.nib 分配视图控制器时,您应该将其“产品”属性绑定到您的数组控制器的选择,它只能在代码中完成,但这将避免需要数组控制器的多个实例,并且避免将它们绑定在一起,使它们看起来一样。

    另外,我建议不要将数组控制器的内容绑定到您自己的 NSArray,如果您不绑定该属性,数组控制器将分配和管理自己的数组。您将能够直接从中添加/删除对象,而不必依赖您自己的属性来仔细通知 NSArrayController 发生了更改。

    “内容”绑定允许将数组控制器的排列对象绑定到另一个控制器的内容,以便能够对内容进行不同的过滤和排序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多