1

It's possible I simply don't understand bindings. Goal: A menu has three options. A toolbar item is a segmented controller that reflects those three items. User can choose any of the three from the menu or the toolbar item. If chosen from the menu, the selectedSegment of the control should reflect the choice.

Added a property to my NSDocument subclass:

class Document: NSDocument {
    var optionSegment: Int = 0

Created a binding from the control to the property. The Bindings Inspector of Storyboard is for "Selected index (Document.optionSegment)" In the action method for the menu items, I set the doc's optionSegment to 0, 1, or 2.

I'd have thought that that would suffice, but nothing happens. Selecting a menu item does not change the control's selectedSegment and clicking the control in the toolbar doesn't change the property.

What might I be missing or not understanding? Tried binding to the toolbaritem's control and control cell, separately and together, and nothing. Not sure what to try next.

Thanks for suggestions.

JKaz
  • 765
  • 6
  • 18
  • Duplicate of [Change bound variable in Swift class](http://stackoverflow.com/questions/36633268/change-bound-variable-in-swift-class) – Willeke Apr 20 '16 at 00:27
  • In hours of searching, never came upon that one. Thanks. Unfortunately, changing to `dynamic var optionSegment: Int = 0` did not change results. Still no this-leads-to-that. – JKaz Apr 20 '16 at 00:46
  • More info. Traces show that the Document subclass was being initialized twice at the launch of the app, though only one window was created. This was caused by the model object I dragged to the scene dock and identified as the Document subclass so that I could reference it in the connection bindings. Maybe that second Document was causing ambiguity with the bindings. So the question is, to what do I bind, as the default choices seem to be Shared User Defaults Controller and Window Controller? – JKaz Apr 20 '16 at 03:34
  • Bind to the window controller, key path `document.optionSegment`. – Willeke Apr 20 '16 at 11:15
  • That works, thank you. While XCode says it can't resolve the path, it still is accurate. There's a side effect though that I don't understand. Just by doing the bindings the view controller no longer deinitializes (and thus a bunch of other classes, too). Without the binding, they deinitialize, and with it they don't. Any idea how to avoid this side effect of the solution? – JKaz Apr 20 '16 at 16:17
  • Whether I subclass the window controller or use a property in the document, binding an NSToolbarItem seems to keep my subclasses of views, view controllers, document, etc from deinitializing. I'll make this a separate question... – JKaz Apr 20 '16 at 18:18
  • Solution at: [link](http://stackoverflow.com/questions/36752294/does-cocoa-connection-binding-to-nstoolbaritem-prevent-deinitializing/36776004#36776004) – JKaz Apr 21 '16 at 18:20

0 Answers0