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

beg_ne

macrumors 6502
Original poster
Jul 3, 2003
452
0
I am currently working through Cocoa Programming for OS X and have reached to the end of Chapter 10 where you work with the User Defaults and set a custom background color for a table view with a pref panel.

So far I have been unable to get that to work! I always get the default background colors. I've checked the API reference and everything is still intact etc.

I have used NSLog to output the values in the MyDocument.m file and it looks like its being set, but I'm not seeing the results in the window.

Here is the code from the -windowControllerDidLoadNib method:
- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
NSData *colorAsData;
[super windowControllerDidLoadNib:aController];

colorAsData = [[NSUserDefaults standardUserDefaults]
objectForKey:BNRTableBgColorKey];

NSLog(@"Current Table Color:%@ : New Table Color Will Be: %@",[tableView backgroundColor],[NSKeyedUnarchiver unarchiveObjectWithData:colorAsData]);

[tableView setBackgroundColor:[NSKeyedUnarchiver
unarchiveObjectWithData:colorAsData]];
NSLog(@"New Table Color:%@",[tableView backgroundColor]);
}


And here is the log output:
2007-12-16 00:28:45.248 RaiseMan[7922:10b] Current Table Color:NSNamedColorSpace System controlBackgroundColor : New Table Color Will Be: NSCalibratedRGBColorSpace 1 0.765541 0.607648 1

2007-12-16 00:28:45.249 RaiseMan[7922:10b] New Table Color:NSCalibratedRGBColorSpace 1 0.765541 0.607648 1


What am I doing wrong???
 
Thanks for the suggest, I've added that but still no luck. Does this work in Leopard at all? I've ever tried messing with the parent NSScrollView and Table view options for the background color in IB3 and I don't get anything other than the default white background.
 
ah ha!

I had the same problem. Turn off "Alternating Rows" on the NSTableView. I know it's cool, but it'll allow the example to work as intended.
 
Dry

NSLog(@"Current Table Color:%@ : New Table Color Will Be: %@",[tableView backgroundColor],[NSKeyedUnarchiver unarchiveObjectWithData:colorAsData]);

[tableView setBackgroundColor:[NSKeyedUnarchiver unarchiveObjectWithData:colorAsData]];

Honestly, I don't understand Cocoa yet (want to learn in the summer), but I can see that it's not a good thing to call the same function twice as you do (ever heard of DRY principle?), and not for performance reason, but for the ability to change the function call in one place only (also makes your code more readable). I would write like this:

userColor = [NSKeyedUnarchiver unarchiveObjectWithData:colorAsData];
NSLog(@"Current Table Color:%@ : New Table Color Will Be: %@",[tableView backgroundColor],userColor);
[tableView setBackgroundColor:userColor];

I remember that rewriting a code to a seemingly equivalent version (like this) once fixed a bug in code I did not understand. But sorry I probably did not solve your problem.
 
but I can see that it's not a good thing to call the same function twice as you do (ever heard of DRY principle?),

One of them is a log (the NSLog one) so will be removed from the final code. So you don't actually want to create a variable for it.
 
I had the same problem. The way I fixed it is go back to Chapter 9 "NSUndoManager" page 150, set File's Owner "window" outlet to "tableView"

Previously I had some difficulty to select "tableView" in Interface Builder (due to my own ignorance).

After that change the color does work.

Check your connections.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.