简体   繁体   中英

bind nsrect and nstextfield

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.textFrame" 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM