여러 토글 버튼이있는 NSmatrix?
-
13-12-2019 - |
문제
0과 4 개의 버튼을 선택할 수있는 NSMatrix
의 NSButtonCell
를 만들려고합니다 (토글링 된).나는 다음 (테스트) 코드를 시도했지만 필요한 기능을 어떻게 제공 할 수 있는지 모르겠습니다.아마도 NSMatrix
에서는 불가능합니다. 그리고 나는 대안적인 통제를보고 내 자신을 만들어야합니까?
@interface MatrixView : NSView
{
NSScrollView *_scrollView;
NSMatrix *_matrixView;
}
@end
@implementation MatrixView
- (id)initWithFrame:(NSRect)frameRect
{
NSLog(@"initWithFrame. frameRect=%@", NSStringFromRect(frameRect));
self = [super initWithFrame:frameRect];
if (self != nil)
{
_scrollView = [[NSScrollView alloc] initWithFrame:NSMakeRect(0, 0, frameRect.size.width, frameRect.size.height)];
[_scrollView setBorderType:NSNoBorder];
[_scrollView setHasVerticalScroller:YES];
[_scrollView setHasHorizontalScroller:NO];
[_scrollView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
NSSize contentSize = [_scrollView contentSize];
contentSize.height = 300;
// Make it 3 x however-many-buttons-will-fit-the-height
CGFloat gap = 8.0;
CGFloat width = (contentSize.width / 3.0) - (gap * 2.0);
NSUInteger rows = (contentSize.height / (width + gap));
NSLog(@"width=%f, rows=%lu", width, rows);
NSButtonCell *prototype = [[NSButtonCell alloc] init];
[prototype setTitle:@"Hello"];
[prototype setButtonType:NSToggleButton];
[prototype setShowsStateBy:NSChangeGrayCellMask];
_matrixView = [[NSMatrix alloc] initWithFrame:NSMakeRect(0, 0, contentSize.width, contentSize.height)
mode:NSListModeMatrix
prototype:prototype
numberOfRows:rows
numberOfColumns:3];
[_matrixView setCellSize:NSMakeSize(width, width)];
[_matrixView setIntercellSpacing:NSMakeSize(gap, gap)];
[_matrixView setAllowsEmptySelection:YES];
[_matrixView sizeToCells];
[_scrollView setDocumentView:_matrixView];
[self addSubview:_scrollView];
[self setAutoresizesSubviews:YES];
[prototype release];
}
return self;
}
...
. 해결책
NSmatrix의 다음 하위 클래스로 작업하려면이 옵션이 있습니다.OnCount, OnCount에 하나의 속성을 추가했습니다.
@implementation RDMatrix
@synthesize onCount;
-(id) initWithParentView:(NSView *) cv {
NSButtonCell *theCell = [[NSButtonCell alloc ]init];
theCell.bezelStyle = NSSmallSquareBezelStyle;
theCell.buttonType = NSPushOnPushOffButton;
theCell.title = @"";
if (self = [super initWithFrame:NSMakeRect(200,150,1,1) mode:2 prototype:theCell numberOfRows:4 numberOfColumns:4]){
[self setSelectionByRect:FALSE];
[self setCellSize:NSMakeSize(40,40)];
[self sizeToCells];
self.target = self;
self.action = @selector(buttonClick:);
self.drawsBackground = FALSE;
self.autoresizingMask = 8;
self.allowsEmptySelection = TRUE;
self.mode = NSHighlightModeMatrix;
self.onCount = 0;
[cv addSubview:self];
return self;
}
return nil;
}
-(IBAction)buttonClick:(NSMatrix *)sender {
NSUInteger onOrOff =[sender.selectedCells.lastObject state];
if (onOrOff) {
self.onCount += 1;
}else{
self.onCount -= 1;
}
NSLog(@"%ld",self.onCount);
if (self.onCount == 5) {
[sender.selectedCells.lastObject setState:0];
self.onCount -= 1;
}
}
.
5 번째 버튼을 선택하려고하면 깜박이면 깜박이거나 꺼집니다.이 버튼의 상태를 사용하는 방법에 따라 이것은 문제 일 수 있습니다.방금이 방법으로 기록했습니다.
-(IBAction)checkMatrix:(id)sender {
NSIndexSet *indxs = [self.mat.cells indexesOfObjectsPassingTest:^BOOL(NSButtonCell *cell, NSUInteger idx, BOOL *stop) {
return cell.state == NSOnState;
}];
NSLog(@"%@",indxs);
}
.
편집 후 : 5 번째 버튼을 클릭하면 먼저 첫 번째 메소드가 짧게 켜기 전에 첫 번째 메소드가 잠시 깜박이면 깜박이는 방식을 좋아하지 않았습니다.나는 매트릭스 서브 클래스에서 mouseDown을 무시하는 더 나은 솔루션이라고 생각하는 것을 발견했습니다 (이를 시도하려는 경우, 설정 및 settarget 문을 삭제하고 buttonClick 메서드를 삭제해야합니다).
-(void)mouseDown:(NSEvent *) event {
NSPoint matPoint = [self convertPoint:event.locationInWindow fromView:nil];
NSInteger row;
NSInteger column;
[self getRow:&row column:&column forPoint:matPoint];
NSButtonCell *cell = [self cellAtRow:row column:column];
if (self.onCount < 4 && cell.state == NSOffState) {
cell.state = NSOnState;
self.onCount += 1;
}else if (cell.state == NSOnState) {
cell.state = NSOffState;
self.onCount -= 1;
}
}
. 제휴하지 않습니다 StackOverflow