I would like to bind frame of NSView to model property NSRect. I did this in that way:
[textView.enclosingScrollView bind:@"frame" toObject:bindingsController withKeyPath:@"selection.text开发者_Go百科Frame" options:nil];
But I also would like to bind frame.origin.x to nstextField. How to do it, with NSValueTransformer?
You can only bind to a property, which means you cannot bind to a certain part of the frame directly. You could do it indirectly either by binding to the frame and using a transformer to get just the x coordinate, or you could add a property to your view which accesses the frame and returns the x coordinate. I would suggest the second method, since you can do it using a category instead of a subclass, and it is easier to allow setting the value.
@interface NSView (FrameXCoordinate)
@property (nonatomic) double frameXCoordinate;
@end
@implementation NSView (FrameXCoordinate)
- (double)frameXCoordinate {
return [self frame].origin.x;
}
- (void)setFrameXCoordinate:(double)x {
NSRect frame = [self frame];
frame.origin.x = x;
[self setFrame:frame];
}
@end
Using this, you would simply bind to the frameXCoordinate
property and add a number formatter to your text field.
精彩评论