无法绑定到NSOutlineView中的NSButton值

时间:2016-04-08 02:51:14

标签: xcode swift cocoa interface-builder

我有一个类,如下:

class OTF:NSObject {
var name:String = ""
var nameID:Int? = 0
var identifier:Int = 0
var isOn:Int! = 1
var search:Int = 0
var selected:Bool! = false

init(name:String, nameID:Int?, identifier:Int) {
    self.name = name
    self.nameID = nameID
    self.identifier = identifier

}}

class OTFeature:OTF {

var selDefault:Int?;
var parent:OTFType!

init (name:String, parent:OTFType, nameID:Int?, identifier:Int, selDefault:Int?) {
    super.init(name: name, nameID: nameID, identifier: identifier)
    self.selDefault = selDefault
    self.parent = parent
}}

当我(在Interface Builder中)将NSOutlineView tableCellButton.objectValue.name绑定到放置在NSTableCellView中的NSButton的标题时,我的按钮具有预期的标题:它没问题。 Delegate和dataSource运行良好。

但是如果我想将tableCellButton.ObjectValue.isOntableCellButton.objectValue.selected绑定到NSButton值,我收到了错误:

[<OTFReview.OTFeature 0x6180000e2b80> valueForUndefinedKey:]: this class is not key value coding-compliant for the key selected.

1 个答案:

答案 0 :(得分:1)

您的“已选择”属性是可选的,因此无法绑定。 您需要将选中的属性设置为非可选并初始化。 此外,您需要将选定的属性称为 动态

尝试下面的代码,看看会发生什么。

class
ViewController: NSViewController {

    dynamic var
    selected : Bool = false

    var
    x : Bool = true

    override func viewDidLoad() {
        super.viewDidLoad()
        bind( "x", toObject: self, withKeyPath: "selected", options: nil )
        print( x )
    }
    @IBAction func
    Do( _: AnyObject ) {
        selected = !selected
        print( x )
    }
}
相关问题