gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 20:25:01 25 4
gpt4 key购买 nike

我想实现一个带有代表自己的图标的广播组。我从 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com