objective c - Set NSButton enabled based on NSArrayController selection -
ok, i've set nstableview
bound nsarraycontroller
.
now, have nsbutton
want make "enabled" when there is selection, , disabled when there nothing selected.
so, i'm bind nsbutton
's enabled array controller's selection
value transformer of nsisnotnil
.
however, doesn't seem working.
am missing anything?
regardless of whether or not selected, selection
property of nsarraycontroller
returns object (_nscontrollerproxyobject
). why binding isn't working way expect, because selection
never nil
. instead, bind selectionindexes
, rather selection
, , have value transformer called selectionindexescountiszero
implemented so:
@interface selectionindexescountiszero : nsvaluetransformer @end @implementation selectionindexescountiszero + (class)transformedvalueclass { return [nsnumber class]; } + (bool)allowsreversetransformation { return no; } - (id)transformedvalue:(nsindexset *)value { return [nsnumber numberwithbool:[value count] > 0]; } @end
incidentally, can still bind selection
if wish, require custom value transformer. apple state that: if value requested selection proxy [object] using key-value coding returns multiple objects, controller has no selection, or proxy not key-value coding compliant requested key, appropriate marker returned. in other words, find out if there in fact no selection, need (i) access proxy object, (ii) call 1 of methods of actual objects, , (iii) test see if return value (ii) nsnoselectionmarker
. doing way key method of value transformer this:
- (id)transformedvalue:(id)selectionproxyobject { // assume objects in table team objects, 'name' property return [selectionproxyobject valueforkeypath:@"name"] == nsnoselectionmarker ? @yes : @no; }
selectionindexes
better way since generic. in fact, if sort of thing lot can idea build transformer library, can import project. here names of of transformers in library:
selectionindexescountiszero selectionindexescountisexactlyone selectionindexescountisoneorgreater selectionindexescountisgreaterthanone // ...you picture
Comments
Post a Comment