绑定 nsrect 和 nstextfield

发布于 2024-12-08 22:33:16 字数 265 浏览 0 评论 0原文

我想将 NSView 的框架绑定到模型属性 NSRect。我这样做是这样的:

[textView.enclosingScrollView bind:@"frame" toObject:bindingsController withKeyPath:@"selection.textFrame" options:nil];

但我也想将frame.origin.x绑定到nstextField。如何使用 NSValueTransformer 做到这一点?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纸伞微斜 2024-12-15 22:33:16

您只能绑定到一个属性,这意味着您不能直接绑定到框架的某个部分。您可以通过绑定到框架并使用转换器来获取 x 坐标来间接完成此操作,也可以向视图添加一个属性来访问框架并返回 x 坐标。我建议使用第二种方法,因为您可以使用类别而不是子类来完成此操作,并且更容易允许设置值。

@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

使用此功能,您只需绑定到 frameXCooperative 属性并将数字格式化程序添加到文本字段即可。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文