One Quickie


View with bindings (Bindings->General)
ThumbBorker has a custom view class that updates a selection rectangle when the user finished dragging the mouse. It uses setValue:forKeyPath: to stick the value back into the bound object.

The bindings and path are ivars:

     id selectionRectBinding;
     NSString *selectionRectKeyPath;
In bind:toObject:withKeyPath:options: hang on to the binding object and the key path and set up observing:
         // hold on to the binding info
         selectionRectBinding = observableObject;
         selectionRectKeyPath = [observableKeyPath copy];

         // connect KVO
         [valuePathBinding addObserver: self
                           forKeyPath: selectionRectKeyPath
                           options: nil
                           context: NULL];

         // new binding, needs to redraw
         [self setNeedsDisplay: YES];
And in the mouseUp: handler, set the value back into the bound object:
     // figure out the selection rectangle
     NSRect selectionRect = [self normalizedSelectionRect];

     // wrap in a value and tell the bound object the new value

     NSValue *value;
     value = [NSValue valueWithRect: selectionRect];

     [selectionRectBinding setValue: value
                           forKeyPath: selectionRectKeyPath];



borkware home | products | miniblog | rants | quickies | cocoaheads
Advanced Mac OS X Programming book

webmonster@borkware.com