我想用一个代表自己的图标来实现一个广播组。我找到了apple的文档:
图标单选按钮
您还可以使用一个单选按钮;也就是说,主要通过其图标识别并且几乎没有文本的一个。如果按钮关闭,它似乎是插入。如果按钮打开,它似乎被按下。(图标按钮不能显示混合状态。)
您可以在Interface Builder中或以编程方式创建一组图标单选按钮。如果使用Interface Builder,请从按钮矩阵开始。如果以编程方式创建它,请创建按钮矩阵。然后将矩阵的跟踪模式更改为NSRadioModeMatrix。将按钮的类型更改为NSPushOnPushOffButton,将其图像位置更改为NSImageOnly,将其边框类型更改为方形边框类型。最后将他们的图像设置为你想要的。
所以,我写了下面的代码:
// 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"]];
但是我得到的并不令人满意,按钮没有边框边框,并且没有选中的state
,我的意思是,我无法分辨当前推入哪一个。
那么我做错了什么?请指出,非常感谢!
答案 0 :(得分:1)
UPDATE *
如果我没有以编程方式完成此操作并按原样执行此操作。
我意识到你还需要在代码中设置按钮单元格的视觉效果
[[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"]];