在Cocoa中创建一个单选按钮矩阵

时间:2011-10-05 20:20:28

标签: objective-c macos cocoa radio-button

我想在OS X应用程序中创建一个包含3个单选按钮的矩阵。有没有人知道如何以编程方式执行此操作?

1 个答案:

答案 0 :(得分:4)

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];
来自Apple文档中Using Radio Buttons

代码段。

请注意原型NSButtonCell的使用方式 - 这样我们就可以告诉NSMatrix应该使用NSRadioButton类型的按钮(这似乎不能只使用单元格的类)