Search code examples
objective-cmacoscocoaradio-button

How to group radio buttons (from storyboard) in cocoa application?


I'm trying to group 2 radio buttons in cocoa application.

but unlike in iOS, where you can connect the buttons with control-drag (as shown in the picture)enter image description here

in cocoa application I didn't find any elegant way to do so...

any suggesting before I do something ugly?


Solution

  • so here is my not very elegant solution -

    1. define array of buttons in the class:

    NSArray* _radioButtonsArray;

    1. initialize it in viewDIdLoad:

      _radioButtonsArray = [[NSArray alloc] initWithObjects:_radioButton1,_radioButton2,_radioButton3, nil];
      
    2. define a radioButtonIsPressed method and connect it to all radio buttons as an action:

      -(IBAction)radioButtonIsPressed:(id)sender{ for (NSButton* btn in _radioButtonsArray){ if (btn != sender) [btn setState:0]; } }