Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos 绑定nsrect和nstextfield_Macos_Cocoa_Operating System - Fatal编程技术网

Macos 绑定nsrect和nstextfield

Macos 绑定nsrect和nstextfield,macos,cocoa,operating-system,Macos,Cocoa,Operating System,我想将NSView的框架绑定到模型属性NSRect。我是这样做的: [textView.enclosingScrollView bind:@"frame" toObject:bindingsController withKeyPath:@"selection.textFrame" options:nil]; 但我还想将frame.origin.x绑定到nstextField。如何使用NSValueTransformer执行此操作?您只能绑定到属性,这意味着您无法直接绑定到帧的特定部分。您可以通

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

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

但我还想将frame.origin.x绑定到nstextField。如何使用NSValueTransformer执行此操作?

您只能绑定到属性,这意味着您无法直接绑定到帧的特定部分。您可以通过绑定到帧并使用转换器仅获取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

使用此选项,您只需绑定到
frameXCoordinate
属性,并将数字格式化程序添加到文本字段。

您只能绑定到属性,这意味着您无法直接绑定到框架的某个部分。您可以通过绑定到帧并使用转换器仅获取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
使用此选项,您只需绑定到
frameXCoordinate
属性,并向文本字段添加数字格式化程序