绑定 nsrect 和 nstextfield
我想将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只能绑定到一个属性,这意味着您不能直接绑定到框架的某个部分。您可以通过绑定到框架并使用转换器来获取 x 坐标来间接完成此操作,也可以向视图添加一个属性来访问框架并返回 x 坐标。我建议使用第二种方法,因为您可以使用类别而不是子类来完成此操作,并且更容易允许设置值。
使用此功能,您只需绑定到
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.
Using this, you would simply bind to the
frameXCoordinate
property and add a number formatter to your text field.