objective c - NSPopUpButton: multiple values & selectedIndex binding -
context:
i have nsarraycontroller tied core data supplies rows nstableview. when user selects rows, arraycontroller's "selectedobjects" property changes.
now, each of "selectedobjects" core data entity called "lpfile" has attribute called "style", integer 0 3. "style" attribute should correspond selectedindex of nspopupbutton.
my question:
if user selects multiple rows , lpfiles associated these rows have same value "style", nspopupbutton set "selectedindex" property value. if rows' objects have different values "style", nspopupbutton should display blank row. (when user chooses style, blank row should disappear nspopupbutton.)
i know how achieve writing code manually , if selection limited single row set bindings, how set bindings handle multiple selected objects may or may not have different values "style"? i've googled quite bit, can't find specific info , i'm tired of experimenting! (note: provide content items nspopupbutton in ib, don't bind content bindings of button.)
you'll have write little bit of code, can still use bindings control ui elements, in case popup button.
here 1 way has worked me:
in controller provides content array controller, define property contains selection index set corresponding selection in table view. bind array controller's selection index set, updated , sync'ed table view. simplicity, have called fileselectionindexset in following.
then, define property provides index popup button. below, have called styleindex.
you can bind popup buttons selection index property. may have provide content controller, too. readonly property returning static array of strings, instance.
// header file, synthezise in implementation @property (retain) nsinteger styleindex; register controller observer of own fileselectionindexset property:
// doesn't have awakefromnib, method if called before // need functionality -(void)awakefromnib { [self addobserver:self forkeypath: @"fileselectionindexset" options:nskeyvalueobservingoptionnew context:null]; } - (void) observevalueforkeypath:(nsstring *)keypath ofobject:(id)object change:(nsdictionary *)change context:(void *)context { if ( [keypath isequaltostring: @"fileselectionindexset"] ) { nsinteger index; index = ... // compute value based on current lpfile selection self.styleindex = index; } } implementing self observer of own property makes styleindex property one-way dependant of fileselectionindexset.
this means whenever user changes selection in table view, popup button updated. however, when user changes selection in popup button, nothing changed in table view.
Comments
Post a Comment