Create a radio button matrix in Cocoa
I want to create a matrix 开发者_如何学JAVAof 3 radio buttons in my OS X app. Does anyone know how to do this programatically?
NSButtonCell *prototype = [[NSButtonCell alloc] init];
[prototype setTitle:@"Watermelons"];
[prototype setButtonType:NSRadioButton];
NSRect matrixRect = NSMakeRect(20.0, 20.0, 125.0, 125.0);
NSMatrix *myMatrix = [[NSMatrix alloc] initWithFrame:matrixRect
mode:NSRadioModeMatrix
prototype:(NSCell *)prototype
numberOfRows:3
numberOfColumns:1];
[[[typeField window] contentView] addSubview:myMatrix];
NSArray *cellArray = [myMatrix cells];
[[cellArray objectAtIndex:0] setTitle:@"Apples"];
[[cellArray objectAtIndex:1] setTitle:@"Oranges"];
[[cellArray objectAtIndex:2] setTitle:@"Pears"];
[prototype release];
[myMatrix release];
code snippet from Using Radio Buttons in Apple's docs.
Note the usage of prototype NSButtonCell
- that way we can tell NSMatrix
that buttons of NSRadioButton
type should be used (which cannot be done using just cell's class it seems)
精彩评论