NSPopupButton在基于视图的NSTableView中:获取绑定工作 [英] NSPopupButton in view based NSTableView: getting bindings to work

查看:272
本文介绍了NSPopupButton在基于视图的NSTableView中:获取绑定工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题描述



我试图实现一些应该是简单和相当普遍的:有一个绑定填充NSPopupButton内部绑定填充NSTableView。 Apple在其文档中描述了基于单元格的表格到作者阵列控制器的解决方法,但这似乎不适用于我


  • 另一个SO问题建议子类化NSTableCellView并以编程方式建立所需的连接。


    $ b

    如果我设置绑定如下:

       - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    NSView * view = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self ];

    if([tableColumn.identifier isEqualToString:@Author){
    AuthorSelectorCell * authorSelectorCell =(AuthorSelectorCell *)view;
    [authorSelectorCell.popupButton bind:NSContentBinding toObject:self.authors withKeyPath:@arrangedObjectsoptions:nil];
    [authorSelectorCell.popupButton bind:NSContentValuesBinding toObject:self.authors withKeyPath:@arrangedObjects.nameoptions:nil];
    [authorSelectorCell.popupButton bind:NSSelectedObjectBinding toObject:view withKeyPath:@objectValue.authoroptions:nil];
    }

    return view;
    }

    弹出窗口会显示可能的作者列表,但当前选择总是显示为没有价值。如果我添加

      [authorSelectorCell.popupButton bind:NSSelectedValueBinding toObject:view withKeyPath:@objectValue.author.nameoptions:nil ]; 

    当前选择完全为空。显示当前选择的唯一方法是设置

      [authorSelectorCell.popupButton bind:NSSelectedObjectBinding toObject:view withKeyPath:@ objectValue.author.nameoptions:nil]; 

    这将在我选择不同的作者后立即中断,因为它会尝试分配一个 NSString * 作者* 属性。


  • 任何想法?

    解决方案

    我已将示例项目显示为可在Github上显示。


    有人建议使用IBOutlet属性给Authors
    数组控制器,但这似乎不适用于我。


    这是 为我工作的方法,这在示例项目。缺点是,IBOutlet到数组控制器需要在提供TableView的委托的类中。


    Problem Description

    I'm trying to achieve something that should be simple and fairly common: having a bindings populated NSPopupButton inside bindings populated NSTableView. Apple describes this for a cell based table in the their documentation Implementing To-One Relationships Using Pop-Up Menus and it looks like this:

    I can't get this to work for a view based table. The "Author" popup won't populate itself no matter what I do.

    I have two array controllers, one for the items in the table (Items) and one for the authors (Authors), both associated with the respective entities in my core data model. I bind the NSManagedPopup in my cell as follows in interface builder:

    • Content -> Authors (Controller Key: arrangedObjects)
    • Content Values -> Authors (Controller Key: arrangedObjects, Model Key Path: name)
    • Selected Object -> Table Cell View (Model Key Path: objectValue.author

    If I place the popup somewhere outside the table it works fine (except for the selection obviously), so I guess the binding setup should be ok.


    Things I Have Already Tried

    1. Someone suggested a workaround using an IBOutlet property to the Authors array controller but this doesn't seem to work for me either.

    2. In another SO question it was suggested to subclass NSTableCellView and establish the required connections programmatically. I tried this but had only limited success.

      If I setup the bindings as follows:

      - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
          NSView *view = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
      
          if ([tableColumn.identifier isEqualToString:@"Author") {
              AuthorSelectorCell *authorSelectorCell = (AuthorSelectorCell *)view;
              [authorSelectorCell.popupButton bind:NSContentBinding toObject:self.authors withKeyPath:@"arrangedObjects" options:nil];
              [authorSelectorCell.popupButton bind:NSContentValuesBinding toObject:self.authors withKeyPath:@"arrangedObjects.name" options:nil];
              [authorSelectorCell.popupButton bind:NSSelectedObjectBinding toObject:view withKeyPath:@"objectValue.author" options:nil];
          }
      
          return view;
      }
      

      the popup does show the list of possible authors but the current selection always shows as "No Value". If I add

      [authorSelectorCell.popupButton bind:NSSelectedValueBinding toObject:view withKeyPath:@"objectValue.author.name" options:nil];
      

      the current selection is completely empty. The only way to make the current selection show up is by setting

      [authorSelectorCell.popupButton bind:NSSelectedObjectBinding toObject:view withKeyPath:@"objectValue.author.name" options:nil];
      

      which will break as soon as I select a different author since it will try to assign an NSString* to an Author* property.

    Any Ideas?

    解决方案

    I had the same problem. I've put a sample project showing this is possible on Github.

    Someone suggested a workaround using an IBOutlet property to the Authors array controller but this doesn't seem to work for me either.

    This is the approach that did work for me, and that is demonstrated in the sample project. The missing bit of the puzzle is that that IBOutlet to the array controller needs to be in the class that provides the TableView's delegate.

    这篇关于NSPopupButton在基于视图的NSTableView中:获取绑定工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆