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 7056495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:52:26+00:00 2026-05-28T03:52:26+00:00

I have set up an NSCollectionView in a cocoa application. I have subclassed the

  • 0

I have set up an NSCollectionView in a cocoa application. I have subclassed the collection view’s NSCollectionViewItem to send me a custom NSNotification when one of its views it selected / deselected. I register to receive a notification within my controller object when this notification is posted. Within this method I tell the view that has just been selected that it is selected and tell it to redraw, which makes it shade itself grey.

The NSCollectionViewItem Subclass:

-(void)setSelected:(BOOL)flag {
[super setSelected:flag];

[[NSNotificationCenter defaultCenter] postNotificationName:@"ASCollectionViewItemSetSelected" 
                                                    object:nil 
                                                  userInfo:[NSDictionary dictionaryWithObjectsAndKeys:(ASListView *)self.view, @"view", 
                                                            [NSNumber numberWithBool:flag], @"flag", nil]];}

The Controller Class (in the -(void)awakeFromNib Method):

//Register for selection changed notification
[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(selectionChanged:) 
                                             name:@"ASCollectionViewItemSetSelected" 
                                           object:nil];

And the -(void)selectionChanged:(NSNotification *)notification method:

- (void)selectionChanged:(NSNotification *)notification {
// * * Must get the selected item and set its properties accordingly

//Get the flag
NSNumber *flagNumber = [notification.userInfo objectForKey:@"flag"];
BOOL flag = flagNumber.boolValue;

//Get the view
ASListView *listView = [notification.userInfo objectForKey:@"view"];

//Set the view's selected property
[listView setIsSelected:flag];
[listView setNeedsDisplay:YES];

//Log for testing
NSLog(@"SelectionChanged to: %d on view: %@", flag, listView);}

The application that contains this code requires there to be no empty selection within the collection view at any time. This is where i get my problem. I’ve tried checking when a view’s selection is changed and reselecting it if there is no selection, and manually selecting the views using NSCollectionView‘s

-(void)setSelectionIndexes:(NSIndexSet *)indexes

But there is always a situation which occurs that causes there to be an empty selection in the collection view.

So I was wondering if there is an easier way to prevent an empty selection occurring in an NSCollectionView? I see no checkbox in interface builder.

Thanks in advance!

Ben

Update

I ended up just subclassing my NSCollectionView, and overriding the - (void)mouseDown:(NSEvent *)theEvent method. I only then sent the method [super mouseDown:theEvent]; if the click was in one of the subviews. Code:

- (void)mouseDown:(NSEvent *)theEvent {
NSPoint clickPoint = [self convertPoint:theEvent.locationInWindow fromView:nil];

int i = 0;
for (NSView *view in self.subviews) {
    if (NSPointInRect(clickPoint, view.frame)) {
        //Click is in rect
        i = 1;
    }
}

//The click wasnt in any of the rects
if (i != 0) {
    [super mouseDown:theEvent];
}}
  • 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-05-28T03:52:27+00:00Added an answer on May 28, 2026 at 3:52 am

    I ended up just subclassing my NSCollectionView, and overriding the - (void)mouseDown:(NSEvent *)theEvent method. I only then sent the method [super mouseDown:theEvent]; if the click was in one of the subviews. Code:

    - (void)mouseDown:(NSEvent *)theEvent {
    NSPoint clickPoint = [self convertPoint:theEvent.locationInWindow fromView:nil];
    
    int i = 0;
    for (NSView *view in self.subviews) {
        if (NSPointInRect(clickPoint, view.frame)) {
            //Click is in rect
            i = 1;
        }
    }
    
    //The click wasnt in any of the rects
    if (i != 0) {
        [super mouseDown:theEvent];
    }}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have set up a Django application that uses images. I think I have
I'm trying to set up an NSCollectionView (I have done this successfully in the
I have set up custom error on my server and i'm redirecting to a
I have set the MaxLength property of a textbox in my windows forms application
I have set up a dialog with several tabs. One of these contains twenty
I have set up a WCF service on a server which lives in its
I have set up some custom symfony project-level settings in the file config/project.yml following
I have set up a logincontrol for my application to authenticate against AD. It
I have set up 2 apache servers, one for 80 port and second for
I have set a custom tint color for a UINavigationBar (within a UINavigationController )

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.