以编程方式创建 Cocoa 单选按钮

发布于 2024-08-06 05:31:41 字数 53 浏览 4 评论 0 原文

我需要以编程方式制作一个 Cocoa 单选按钮,任何人都可以解释如何做到这一点或如何做得好?

I need to make a Cocoa radio button programmatically, can anyone explain how this might be done or how to do this well?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橘味果▽酱 2024-08-13 05:31:41

以下是以编程方式创建单选按钮的示例代码:

//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's a sample code for creating radio buttons programmatically:

//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];

Have fun! Yes, this is taken from here, but i added some personal comments to explain the process.

随风而去 2024-08-13 05:31:41

摘自此处:

单选按钮实际上是按钮单元格的矩阵。独家的
选择性是矩阵的一个属性。

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

另请参阅此链接,了解有关如何以编程方式制作 NSMatrix 的更多示例代码。

Taken from here:

Radio buttons are in fact a matrix if button cells. The exclusive
selectivity is a property of the matrix.

To programmatically create a matrix of button cells you do exactly the same
operations programmatically that IB does as the result of your input. e.g.
Create an NSMatrix instance, set its cell prototype to an NSButtonCell, set
that attributes of the matrix via its public methods (the same ones IB
uses), and set the attributes of the prototype button cell and or all of the
contained button cells.

Also see this link for more sample code on how to make an NSMatrix programatically.

荭秂 2024-08-13 05:31:41

NSButton 类参考位于此处

按钮编程主题,特别是单选按钮(带有示例代码)
可以在这里找到

The NSButton Class reference is here

Button programming topics, specifically Radio Button (with sample code)
can be found here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文