2009-09-21 125 views

回答

9

下面是一个示例代码编程创建单选按钮:

//create the radio button prototype 
NSButtonCell *proto = [[NSButtonCell alloc] init]; 
[proto setTitle:@"Options"]; 
[proto setButtonType: NSRadioButton]; 

//define the matrix size where you'll put the radio buttons 
NSRect matrixRect = NSMakeRect(20.0,20.0,125.0,125.0); 

//define the matrix specifying that it will contain radio buttons of 
//prototype "proto" defined above, and that it will have 3 radio buttons 
//arranged on 1 column 
NSMatrix *matrix = [[NSMatrix alloc] initWithRect: matrixRect 
            mode: NSRadioModeMatrix 
            prototype: (NSCell *)proto 
            numberOfRows:3 numberOfColumns:1]; 

//this assumes that you connected the window object to an outlet 
[[windowOutlet contentView] addSubview: matrix]; 

//set the radio buttons' titles by getting references to the matrix's cells 
NSArray *cells = [matrix cells]; 
[[cells objectAtIndex:0] setTitle:@"Option 1"]; 
[[cells objectAtIndex:1] setTitle:@"Option 2"]; 
[[cells objectAtIndex:2] setTitle:@"Option 3"]; 

[proto release]; 
[matrix release]; 

玩得开心!是的,这是取自here,但我添加了一些个人意见来解释过程。

+0

非常好,谢谢。为什么Matrix编程指南中没有这个代码? – rocky 2013-11-20 20:55:02

4

here:两者

单选按钮实际上如果纽扣电池的矩阵。独有的选择性是矩阵的一个属性。

要以编程方式创建一个按钮单元矩阵,您可以按照您的输入结果以IB的形式执行完全相同的 操作。例如 创建NSMatrix实例,将其单元格原型设置为NSButtonCell,设置 通过其公共方法(与IB 使用相同的方法)设置矩阵的属性,并设置原型按钮单元的属性和/或所有的 纽扣电池。

另请参阅this link了解更多示例代码,了解如何以编程方式制作NSMatrix。

相关问题