Obj-c: Multiple NSMatrix - determining which one is being clicked on..

I am sure this is a simple answer that I am not seeing. My app is for multiple users. I have two matrixes of radio buttons that point to one method. (It’s a gender selection). How do I determine which matrix was being used. I’ve tried this:

-(IBAction)radioButton:(id)sender {
NSMatrix *radioMatrix = sender;
NSButtonCell *radioButton = [radioMatrix selectedCell];
NSLog(@“%@”, [radioMatrix Name]);
}

in various ways, radiomatrix.name, radioMatrix Object ID, sender.name, etc. but they all fail. How do I get the properties of a matrix?

Hi,

the simplest way is to tag (an integer) the matrices in Interface Builder,
for example assign 0 to the first matrix, 1 to the second etc.

Define an enumeration, this is optional but increases the readability of the code

enum { kMyFirstMatrix = 0, kMySecondMatrix, kMyThridMatrix, }; typedef NSUInteger MatrixIdentifier;
then it’s very easy to identify the matrices


-(IBAction)radioButton:(id)sender {
    MatrixIdentifier identifier = [sender tag];
    if (identifier == kMyFirstMatrix)
        // do something;
    else if (identifier == kMySecondMatrix)
        // do something else;
}

I solved it using tag:

if ([radioMatrix tag] == 0) {
//do something coming from matrix 1
} else {
//do something coming from matrix 2
}

Hi Stefan…

Should have checked here before I posted. I figured it out exactly as you suggested. Question though, why can I set Name and Object ID in IB but not access those properties?

Rereading your post, are you discouraging setting the tag in IB for matrices? (Which is what I did)

No, why? I just prefer better identifiers than a literal number.

All information in the AppleScript tab in Interface Builder are not accessible from Objective-C
Read NSMatrix Class Reference to see the possible methods