问题描述
我正在学习KVC和绑定。目前,我试图绑定一个NSTextField到计算属性 colorWallStr
。我已经将滑块的值绑定到相应的颜色变量,并将标签的值绑定到computed属性。
但是,移动幻灯片时,标签的内容不会改变。
// MainWindowController内部
动态var colorRed:CGFloat = 1.0
动态var colorGreen:CGFloat = 1.0
动态var colorBlue:CGFloat = 0.0
dynamic var colorWallStr:String {
get {
returnR:\(colorRed)G:\(colorGreen)B:\(colorBlue)
}
}
当我将标签直接链接到颜色变量时,它工作正常。
感谢@ vadian的回答。现在我可以使用属性的 didSet
更新标签来触发更新标签方法(见下文)。
dynamic var colorBlue:CGFloat = 0.0 {
didSet {
updateLabel()
}
}
func updateLabel
colorWall =R:\(colorRed)G:\(colorGreen)B:\(colorBlue)
}
如果字符串插值中使用的属性不更新包含的计算属性,那么为什么以下代码段不起作用?
dynamic var colorWall:String {
pre>
get {
let red = colorRed
let green = colorGreen
let blue = colorBlue
returnR:\(red)G:\(green)B:\(blue)
}
}
解决方案Key-Value Observering API允许您注册 em>。下面是文档介绍主题的方法:
在这种情况下,
colorWallString
的值对你的三个颜色变量的值,所以你需要做的是实现一个类方法,使这一点清楚://这是非常重要的,你得到这个方法的签名正确,
//否则会被忽略。
class func keyPathsForValuesAffectingColorWallStr() - >设置< NSObject> {
return set< NSObject>(arrayLiteral:colorRed,colorBlue,colorGreen)
}
如代码片段中所述,用于标记从属键的方法的格式至关重要;您可以(而且应该)阅读。
I am learning KVC and binding. Currently, I am trying to bind an NSTextField to a computed property
colorWallStr
. I have bound the sliders' value to the corresponding color variable and also bound the value of the label to the computed property.However, the content of the label does not change when I move the slide.
// Inside MainWindowController dynamic var colorRed: CGFloat = 1.0 dynamic var colorGreen: CGFloat = 1.0 dynamic var colorBlue: CGFloat = 0.0 dynamic var colorWallStr: String { get { return "R: \(colorRed) G: \(colorGreen) B: \(colorBlue)" } }
It is working fine when I bond the label to the color variable directly.
Thanks @vadian's answer. Now I can update the label using property's
didSet
to trigger update label method (see below).dynamic var colorBlue: CGFloat = 0.0 { didSet { updateLabel() } } func updateLabel() { colorWall = "R: \(colorRed) G: \(colorGreen) B: \(colorBlue)" }
If properties used in string interpolation don't update the enclosing computed property, then why does the following code snippet does not work?
dynamic var colorWall: String { get { let red = colorRed let green = colorGreen let blue = colorBlue return "R: \(red) G: \(green) B: \(blue)" } }
解决方案The Key-Value Observering API lets you handle situations like this by allowing you to register dependent keys. Here's how the documentation introduces the subject:
In this situation the value of
colorWallString
depends on the value of your three color variables, so all you need to do is implement a class method that makes this clear:// It's crucial that you get the signature of this method correct, // otherwise it'll just be ignored. class func keyPathsForValuesAffectingColorWallStr() -> Set<NSObject> { return Set<NSObject>(arrayLiteral: "colorRed", "colorBlue", "colorGreen") }
As noted in the code snippet, the format of the method you use to flag up dependent keys is crucial; you can (and should) read the relevant documentation here.
这篇关于Swift:计算属性的Cocoa绑定值不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!