Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind NSButton state

I'm trying to bind the state of a NSButton to an objectController, but I can't find in Interface Builder the Voice "State" under Binds for the button.

Is there a way to bind this property?

like image 411
MatterGoal Avatar asked Jul 26 '11 15:07

MatterGoal


2 Answers

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

like image 135
Rob Napier Avatar answered Sep 29 '22 03:09

Rob Napier


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)
like image 45
Ian Bytchek Avatar answered Sep 29 '22 05:09

Ian Bytchek