Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
i'm attempting to programatically change the background color of my NSCollectionView.

first i subcleassed it = CollectionBackground : NSCollectionView, and added the IBAction methods to the header:

Code:
#import <Cocoa/Cocoa.h>

@interface CollectionBackground : NSCollectionView
	{

	}

- (IBAction)backgroundColorBlack:(id)sender;
- (IBAction)backgroundColorGray:(id)sender;
- (IBAction)backgroundColorWhite:(id)sender;

@end

then i added the actions to the implementation file:

Code:
#import "CollectionBackground.h"

@implementation CollectionBackground

- (IBAction)backgroundColorBlack:(id)sender
	{
	NSLog (@"Background Tunred Black");
	[super setBackgroundColors:[NSArray arrayWithObjects:[NSColor blackColor], nil]];
	}
	
- (IBAction)backgroundColorGray:(id)sender
	{
	NSLog (@"Background Tunred Gray");
	[super setBackgroundColors:[NSArray arrayWithObjects:[NSColor grayColor], nil]];
	}
	
- (IBAction)backgroundColorWhite:(id)sender
	{
	NSLog (@"Background Tunred White");
	[super setBackgroundColors:[NSArray arrayWithObjects:[NSColor whiteColor], nil]];
	}

@end

except for the NSLogs, nothing is happening... is this the proper way to do this?
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Just guessing here since I've never used NSCollectionView, but do you actually have views in the collection? I'm thinking the background colors kind of work like the alternative color on the table view - you actually need to provide the data with view for the colors to show.
 

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
yeah there's a collectionViewItem object and a view that represents each of the views for the collection...

so do i just add these actions to the collectionViewItem object?

i really don't get this...
 

kpua

macrumors 6502
Jul 25, 2006
294
0
Could your views be completely opaque and filling the entire collection view so that background doesn't show? If not, what does the background look like?
 

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
ive attached a small working example... i think what i'm doing is the wrong way of doing this, clearly, since it doesn't work... but maybe i'm just forgetting something?

in IB, you'll notice that the NSCollectionView is nested inside an NSScrollView (it's like that by default)... the background of the collectionView is Blue on launch, so it's not opaque...
 

Attachments

  • ColViewBackColorChange.zip
    56.3 KB · Views: 147

kpua

macrumors 6502
Jul 25, 2006
294
0
Take a closer look at your nib. It's set up totally wrong.

Your NSCollectionView subclass "CollectionBackground" is a top-level object in the nib, not in the view like it should be. Instead of dragging out a new collection view, you should have opened the inspector, selected the existing collection view, and changed its class to CollectionBackground.

The button actions are tied to the CollectionBackground instance, so it's really doing the work when you click the buttons, but since it's not in a view, you're not seeing anything. Connect the buttons to the collection view in the window after you changed its class. This makes it work for me.

Is this your first time using IB, or was this just a mental lapse? You may need some brushing up...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.