objective-c - 如何实现图标单选组?

标签 objective-c xcode macos cocoa appkit

我想实现一个带有代表自己的图标的广播组。我从 apple 找到了文档:

Icon Radio Buttons

You can also have a radio button that’s an icon button; that is, one that’s primarily identified by its icon and has little or no text. If the button’s off, it appears to be sticking in. If the button’s on, it appears to be pressed in. (An icon button cannot display the mixed state.)

You can create an group of icon radio buttons in either Interface Builder or programmatically. If you use Interface Builder, start with a matrix of push buttons. If you create it programmatically, create an matrix of buttons. Then change the matrix’s tracking mode to NSRadioModeMatrix. Change the buttons’ types to NSPushOnPushOffButton, their image positions to NSImageOnly, their bezel types to a square bezel type. Finally set their images to what you want.

所以,我写了下面的代码:

// self.matrix is bound in IB whose tracking mode is NSRadioModeMatrix
NSArray *cellArray = [self.matrix cells];
// Set the button type to NSPushOnPushOffButton
[[cellArray objectAtIndex:0] setButtonType:NSPushOnPushOffButton];
// Set image position to NSImageOnly
[[cellArray objectAtIndex:0] setImagePosition:NSImageOnly];
// Set bezel type to square bezel type
[[cellArray objectAtIndex:0] setBezelStyle:NSThickSquareBezelStyle];
// Finally set the image
[[cellArray objectAtIndex:0] setImage:[NSImage imageNamed:@"ImageA"]];
// Do it all over again
[[cellArray objectAtIndex:1] setButtonType:NSPushOnPushOffButton];
[[cellArray objectAtIndex:1] setImagePosition:NSImageOnly];
[[cellArray objectAtIndex:1] setBezelStyle:NSThickSquareBezelStyle];
[[cellArray objectAtIndex:1] setImage:[NSImage imageNamed:@"DensityMax"]];

但我得到的结果并不令人满意,按钮没有边框,也没有选择的状态,我的意思是,我无法判断当前按下了哪个。

enter image description here

那我做错了什么?请帮我指出来,非常感谢!

最佳答案

更新*

如果我不以编程方式完全执行此操作,而是按照您的方式执行。

我意识到你还需要在代码中将按钮单元格的视觉设置为带边框

[[cellArray objectAtIndex:0] setBordered:YES];

完全以编程方式创建 Matrix 似乎不需要您设置的边框:

 NSRect windowContentViewRect = NSMakeRect(_cView.frame.origin.x +10, _cView.frame.origin.y+150, _cView.frame.size.width, _cView.frame.size.height);
NSMatrix* matrix = [[NSMatrix alloc] initWithFrame:windowContentViewRect mode:NSTrackModeMatrix cellClass:[NSButtonCell class] numberOfRows:2 numberOfColumns:1];

[matrix setCellSize:NSMakeSize(100, 100)];
[matrix sizeToCells];
[matrix setNeedsDisplay:YES];

[_cView addSubview: matrix  ];

[_cView setNeedsDisplay:YES];

NSArray *cellArray = [matrix cells];
// Set the button type to NSPushOnPushOffButton
[[cellArray objectAtIndex:0] setButtonType:NSPushOnPushOffButton];
// Set bezel type to square bezel type
[[cellArray objectAtIndex:0] setBezelStyle:NSThickSquareBezelStyle];
// Set image position to NSImageOnly
[[cellArray objectAtIndex:0] setImagePosition:NSImageOnly];

// Finally set the image
[[cellArray objectAtIndex:0] setImage:[NSImage imageNamed:@"ImageA"]];


// Do it all over again
[[cellArray objectAtIndex:1] setButtonType:NSPushOnPushOffButton];
[[cellArray objectAtIndex:1] setBezelStyle:NSThickSquareBezelStyle];
[[cellArray objectAtIndex:1] setImagePosition:NSImageOnly];

[[cellArray objectAtIndex:1] setImage:[NSImage imageNamed:@"DensityMax"]];

enter image description here

关于objective-c - 如何实现图标单选组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21641204/

相关文章:

macos - MonoDevelop 的 ~/.config/MonoDevelop/在哪里

iphone - 如果 friend 在应用程序中的进度还不足以到达 installInvitationHandler 方法,如何处理游戏中心邀请?

ios - UIAlertViewController 在模拟器中打开慢速动画后没有响应

c++ - 欧拉计划问题 4 的错误解决方案

ios - 用于签署可执行文件的身份不再有效 (iOS 8+/Xcode 7)

macos - 在 OS X 应用程序上嵌入 CouchDB(零部署)

objective-c - present Modal ViewController 给出黑屏

objective-c - 更改 10.6.x 中的桌面图像

ios - Xcode:无法解析 .. 的调试映射是一个目录

python - PIL 中的 PNG 显示在 OS X Mavericks 上损坏了吗?