这里是我不太优雅的解决方案 -
NSArray* _radioButtonsArray;
initialize it in viewDIdLoad:
_radioButtonsArray = [[NSArray alloc] initWithObjects:_radioButton1,_radioButton2,_radioButton3, nil];
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];
}
}
我使用控制拖动单选按钮来创建一个IBAction。然后我将单选按钮复制/粘贴到同一个视图上。现在有两个具有相同IBAction的单选按钮。Cocoa将它们视为分组。