For cases like this I strongly recommend using Cocoa Bindings; this is actually one of the simpler ways to use bindings.
With bindings, user interface synchronization is handled for you. This means that you don't really need to query the state of a radio button; you just query the property that the button is bound to.
Instead of implementing action methods, you only need properties. For instance, - (BOOL) radio1;
, - (void) setRadio1:(BOOL) flag;
, - (BOOL) radio2;
and - (void) setRadio2:(BOOL) flag;
(but give the methods better names than that). You can use @property
for these in later versions of Objective-C. Put BOOL
fields in your class for each one.
Properties can be assigned to radio buttons when you're editing your NIB/XIB file.
By the rules of key-value coding, self.radio1
is a path that implicitly means that the methods radio1
and setRadio1
are both called. If you renamed them to something else, adjust the path name accordingly.
When editing your NIB/XIB, select each Button Cell of your NSMatrix in turn and set an appropriate binding:
- Set the Value binding to "File's Owner" (if that's where the properties are implemented).
- Use the key path of the appropriate property (
self.radio1
or self.radio2
, if you had the above example methods).
The NSMatrix is already set up to allow only one value at a time, so the property values will be similarly restricted.