2009-09-21 73 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。

相關問題