Bind NSButton state
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)
精彩评论