我想在OS X应用程序中创建一个包含3个单选按钮的矩阵。有没有人知道如何以编程方式执行此操作?
答案 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
类型的按钮(这似乎不能只使用单元格的类)