0

According to Big Nerd Ranch book "Cocoa programming for OS X" old way of using NSMatrix is going to be deprecated. New way is to use several instances of NSButton with style set to Radio. Each of NSButton from the same radio group should be assigned to one shared @IBAction method. And use tag property to distinguish them in this action method.

Question is: How currently checked NSButton from radio group gets notification that another button was clicked, and current button should change its state to unchecked?

I know about question: How to create NSRadioButton Group in Xcode 7 OSX but my question is not about how to create it. Question is how message reaches other NSButton views? Because Action is executed on the Window controller class, not on the views.

Picture is follow:

        --> NSButton sends @IBAction ViewController.radioButtonPressed 
Window -| 
        --> NSButton sends @IBAction ViewController.radioButtonPressed

But how this two instances exchange state information between themselves? It seems that there are only one way communication between them and ViewController? How one button know that another was pressed?

Community
  • 1
  • 1
Yury Kochubeev
  • 177
  • 1
  • 1
  • 12
  • Create a variable to keep the current state (tag or reference to the button). If the state changes set the button representing the current state to OFF, set the sender to ON and keep the sender as current state – vadian Apr 18 '16 at 21:12
  • Thank you for your help. I've created to radio buttons which works quite well, but i want to understand how works under the hood. How two separate views gets information about their state, while they could only send messages to viewController class. – Yury Kochubeev Apr 18 '16 at 21:18

1 Answers1

1

The button is in the view hierarchy, so it can ask for its superview. Then it can iterate the superviews subviews, look for NSButton of the type radio button and compare action selector to build a group.

catlan
  • 25,100
  • 8
  • 67
  • 78