添加复合属性以绑定到现有类的最佳方法是什么

时间:2021-04-18 19:37:21

Let's say I have a Size class which has height and width properties (in reality the class is a bit more complex than this, but Size makes a good example).

假设我有一个具有高度和宽度属性的Size类(实际上这个类比这个更复杂,但是Size就是一个很好的例子)。

I want to display this as $width x $height in my UI.

我希望在我的UI中将其显示为$ width x $ height。

The obvious way to do this is to bind to a dimensions property which is dependent on width and height.

显而易见的方法是绑定到一个维度属性,该属性取决于宽度和高度。

My question is where is the best place to add this new property?

我的问题是,添加这个新房产的最佳位置在哪里?

I could add it to the Size class itself in the modal, but then if another controller wants to display the string different I'm stuck creating yet another property. I'm also a bit reluctant to do this because in my case the Size class is in a framework that will be used in a couple different places (although likely all from code I have control over).

我可以在模态中将它添加到Size类本身,但是如果另一个控制器想要显示不同的字符串,那么我就会创建另一个属性。我也有点不愿意这样做,因为在我的情况下,Size类是在一个框架中,将在几个不同的地方使用(尽管可能都来自我控制的代码)。

I could add it in a category to the Size class inside the project containing the view/controller so it will only be availiable in the places I know it will be used, but in various places I've seen suggestions that categories tend to be overused, and am forced to wonder if this is one of those cases.

我可以将它添加到包含视图/控制器的项目内的Size类的类别中,因此它只能在我知道它将被使用的地方使用,但在各个地方我看到类别往往被过度使用的建议,我不得不怀疑这是否是其中之一。

In the case of a single Size instance I could create the property in the controller class containing it, but this becomes more difficult when you have an array of Sizes.

在单个Size实例的情况下,我可以在包含它的控制器类中创建属性,但是当你有一个Sizes数组时,这会变得更加困难。

I could bind to the entire size object and use a transformer to turn them into strings, but binding to an array of sizes would then cause you to have to transform each element of the array into a new array in the transformer, which seems a bit ugly.

我可以绑定到整个尺寸对象,并使用变压器来将它们转换为字符串,但随后结合大小的数组会导致你有到阵列中的每个元素转变成在变压器一个新的数组,这似乎有点丑陋。

3 个解决方案

#1


If want to display this composite value as a string, then bind "Display Pattern 1" of a text field to the width property and "Display Pattern 2" (shown when you bind Display Pattern 1) to the height property. In the Display Pattern 1 binding, set the "Display Pattern" to %{value1}@ x %{value2}@ (yes, slightly unintuitive syntax). This will give a text field that displays "[width] x [height]" and will update on changes in either property.

如果要将此复合值显示为字符串,则将文本字段的“显示模式1”绑定到width属性,将“显示模式2”(将显示模式1绑定时显示)绑定到height属性。在显示模式1绑定中,将“显示模式”设置为%{value1} @ x%{value2} @(是的,稍微不直观的语法)。这将给出一个显示“[width] x [height]”的文本字段,并将更新任一属性中的更改。

You can do the same with an NSTextFieldCell, e.g. as the cell in an NSTableColumn. The downside of this method is that the text field will not be able to edit the bound width and height values. You would have to write an NSValueTransformer if you need to be able to edit them.

您可以使用NSTextFieldCell执行相同操作,例如作为NSTableColumn中的单元格。此方法的缺点是文本字段将无法编辑绑定的宽度和高度值。如果您需要编辑NSValueTransformer,则必须编写NSValueTransformer。

#2


Using multiple bindings through a display pattern as Barry suggested sounds like the best approach, at least without knowing more about your UI. I've used the same thing in the past, where I built an inspector for an array of images and had the dimensions bound to a single text field.

通过显示模式使用多个绑定,因为Barry建议听起来像是最好的方法,至少在不了解您的UI的情况下。我过去曾经使用过同样的东西,在那里我为一组图像构建了一个检查器,并将尺寸绑定到一个文本字段。

In general it's good practice to use value transformers or formatters if you can, but in cases of last resort there's nothing wrong with using a category. I've done this in the past when I had trouble binding to a date, but breaking it down into individual time and date pieces. A category is a good approach because it lets you maintain separation with the model, but you don't need to do anything crazy like binding directly to the controller.

一般来说,如果可以的话,最好使用价值变换器或格式化器,但在最后的情况下,使用类别没有任何问题。我在过去遇到日期时遇到了麻烦,但是把它分解成了个别的时间和日期。类别是一种很好的方法,因为它可以让你保持与模型的分离,但你不需要做任何疯狂的事情,比如直接绑定到控制器。

#3


I want to display this as $width x $height in my UI.

我希望在我的UI中将其显示为$ width x $ height。

Why not two fields? Then you could make them editable.

为什么不是两个领域?然后你可以让它们可编辑。

(in reality the class is a bit more complex than this, but Size makes a good example)

(实际上这个类比这个更复杂,但是Size就是一个很好的例子)

Assuming the above is not feasible in your real situation, you might try creating a custom subclass of NSFormatter, and setting it as the formatter on the cell (I assume this is in a table view, since you wouldn't bind a single control to an array). You would then bind to whole Size objects.

假设上述情况在您的实际情况下不可行,您可以尝试创建NSFormatter的自定义子类,并将其设置为单元格上的格式化程序(我假设这是在表视图中,因为您不会将单个控件绑定到数组)。然后,您将绑定到整个Size对象。

#1


If want to display this composite value as a string, then bind "Display Pattern 1" of a text field to the width property and "Display Pattern 2" (shown when you bind Display Pattern 1) to the height property. In the Display Pattern 1 binding, set the "Display Pattern" to %{value1}@ x %{value2}@ (yes, slightly unintuitive syntax). This will give a text field that displays "[width] x [height]" and will update on changes in either property.

如果要将此复合值显示为字符串,则将文本字段的“显示模式1”绑定到width属性,将“显示模式2”(将显示模式1绑定时显示)绑定到height属性。在显示模式1绑定中,将“显示模式”设置为%{value1} @ x%{value2} @(是的,稍微不直观的语法)。这将给出一个显示“[width] x [height]”的文本字段,并将更新任一属性中的更改。

You can do the same with an NSTextFieldCell, e.g. as the cell in an NSTableColumn. The downside of this method is that the text field will not be able to edit the bound width and height values. You would have to write an NSValueTransformer if you need to be able to edit them.

您可以使用NSTextFieldCell执行相同操作,例如作为NSTableColumn中的单元格。此方法的缺点是文本字段将无法编辑绑定的宽度和高度值。如果您需要编辑NSValueTransformer,则必须编写NSValueTransformer。

#2


Using multiple bindings through a display pattern as Barry suggested sounds like the best approach, at least without knowing more about your UI. I've used the same thing in the past, where I built an inspector for an array of images and had the dimensions bound to a single text field.

通过显示模式使用多个绑定,因为Barry建议听起来像是最好的方法,至少在不了解您的UI的情况下。我过去曾经使用过同样的东西,在那里我为一组图像构建了一个检查器,并将尺寸绑定到一个文本字段。

In general it's good practice to use value transformers or formatters if you can, but in cases of last resort there's nothing wrong with using a category. I've done this in the past when I had trouble binding to a date, but breaking it down into individual time and date pieces. A category is a good approach because it lets you maintain separation with the model, but you don't need to do anything crazy like binding directly to the controller.

一般来说,如果可以的话,最好使用价值变换器或格式化器,但在最后的情况下,使用类别没有任何问题。我在过去遇到日期时遇到了麻烦,但是把它分解成了个别的时间和日期。类别是一种很好的方法,因为它可以让你保持与模型的分离,但你不需要做任何疯狂的事情,比如直接绑定到控制器。

#3


I want to display this as $width x $height in my UI.

我希望在我的UI中将其显示为$ width x $ height。

Why not two fields? Then you could make them editable.

为什么不是两个领域?然后你可以让它们可编辑。

(in reality the class is a bit more complex than this, but Size makes a good example)

(实际上这个类比这个更复杂,但是Size就是一个很好的例子)

Assuming the above is not feasible in your real situation, you might try creating a custom subclass of NSFormatter, and setting it as the formatter on the cell (I assume this is in a table view, since you wouldn't bind a single control to an array). You would then bind to whole Size objects.

假设上述情况在您的实际情况下不可行,您可以尝试创建NSFormatter的自定义子类,并将其设置为单元格上的格式化程序(我假设这是在表视图中,因为您不会将单个控件绑定到数组)。然后,您将绑定到整个Size对象。