开发者

Bind NSButton state

开发者 https://www.devze.com 2023-03-22 20:24 出处:网络
I\'m trying to bind the state of a NSButton to an objectController, but I can\'t fin开发者_StackOverflow中文版d in Interface Builder the Voice \"State\" under Binds for the button.

I'm trying to bind the state of a NSButton to an objectController, but I can't fin开发者_StackOverflow中文版d in Interface Builder the Voice "State" under Binds for the button.

Is there a way to bind this property?


I assume this is a checkbox-style NSButton? Bind to its "value" in IB.


In case anyone wants to do two-way binding between NSButton's state and, say, NSUserDefaultsController in Swift 2, here's how you can do this. All kudos to this answer.

var button: NSButton!
let userDefaults: NSObject = NSUserDefaultsController.sharedUserDefaultsController().values as! NSObject
let options: [String:AnyObject] = [NSContinuouslyUpdatesValueBindingOption: true]
button.cell!.bind("state", toObject: userDefaults, withKeyPath: "MyButtonState", options: options)
userDefaults.bind("MyButtonState", toObject: button.cell!, withKeyPath: "state", options: options)
0

精彩评论

暂无评论...
验证码 换一张
取 消