Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7981551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:24:38+00:00 2026-06-04T10:24:38+00:00

I am trying to create an NSMatrix of NSButtonCell s where between zero and

  • 0

I am trying to create an NSMatrix of NSButtonCells where between zero and four buttons can be selected (toggled on). I have tried the following (test) code, but am not sure how I can provide the functionality I require. Perhaps it’s not possible with NSMatrix and I need to look at an alternative control, or create my own?

@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;
}

...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T10:24:39+00:00Added an answer on June 4, 2026 at 10:24 am

    I got this to work with the following subclass of NSMatrix. I added one property, onCount, to keep track of how many buttons were in the on state:

    @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;
        }    
    }
    

    When you try to select the 5th button it will flash on, but then go off. This could be a problem depending on how you are using the state of these buttons. I just logged them with this method:

    -(IBAction)checkMatrix:(id)sender {
        NSIndexSet *indxs = [self.mat.cells indexesOfObjectsPassingTest:^BOOL(NSButtonCell *cell, NSUInteger idx, BOOL *stop) {
            return cell.state == NSOnState;
        }];
        NSLog(@"%@",indxs);
    }
    

    After Edit: I didn’t like the way my first method flashed the button on briefly before turning it off again when you try to click the 5th button. I found what I think is a better solution that involves overriding mouseDown in the matrix subclass (if you want to try this, you should delete the setAction and setTarget statements and delete the buttonClick method):

    -(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;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
Trying to create a macro which can be used for print debug messages when
Trying to create a generic video plugin for Expression Engine 2 where I can
hi I'm trying create chat using node.js I see example in http://chat.nodejs.org/ I have
I'm trying create a RCP Application with Eclipse, but I can't get past the
I'm trying create a ASMX webservice that can perform a HTTP GET request. I
I am trying create a 'live' view where our clients can edit the colors
Trying to create the following routine in MySQL Workbench yields a This object's DDL
Trying to create a pattern that matches an opening bracket and gets everything between
Trying to create a QtRuby application, I get the following error: /usr/lib64/ruby/site_ruby/1.8/Qt/qtruby4.rb:2144: [BUG] Segmentation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.