NSPopupButton in view based NSTableView: getting bindings to work

妖精的绣舞 提交于 2019-12-03 14:38:25

问题


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?


回答1:


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.




回答2:


Had the same problem and found this workaround - basically get your authors array controller out of nib with a IBOutlet and bind to it via file owner.




回答3:


You can try this FOUR + 1 settings for NSPopUpbutton:

In my example, "allPersons" is equivalent to your "Authors". I have allPersons available as a property (NSArray*) in File's owner.

Additionally, I bound the tableView delegate to File's owner. If this is not bound, I just get a default list :Item1, Item2, Item3



来源:https://stackoverflow.com/questions/14708947/nspopupbutton-in-view-based-nstableview-getting-bindings-to-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!