开发者

bind nsrect and nstextfield

开发者 https://www.devze.com 2023-04-11 15:12 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消