I have an NSTextField
, an NSTableView
with a column "category", an NSColorWell
, and a custom array controller.
The NSTextField
and the column "category" from the NSTextField
are bound to the array controller so that everything I write in the text field shows up in this column.
The problem I have is the following:
I want the the string in the "category" column to get the color of the NSColorWell
when the user chooses a color. All that works fine, but when I open the app a second time and I create a second entry, then the color of the string in the "category" column goes back to the first color it had before the user chose a color.
The way I'm trying this at the moment:
AppDelegate.h :
IBOutlet NSArrayController *doToItemsArrayController;
NSColorWell *colorWell;
- (IBAction)addNewToDoItem:(id)sender;
@property (assign) IBOutlet NSColorWell *colorWell;
AppDelegate.m :
@synthesize colorWell;
- (IBAction)addNewToDoItem:(id)sender
{
ToDoItem *newToDo = [[ToDoItem alloc] init];
[newToDo setCategory:@"Beispiel-Kategorie"];
[newToDo setColor:[NSColor greenColor]];
[doToItemsArrayController addObject:newToDo];
}
- (IBAction)colorWellColorChanged:(id)sender
{
NSColor *currentColor = [colorWell color];
NSArray *selectedItems = [doToItemsArrayController selectedObjects];
// Allen Items die Farbe geben
// Es können ja auch mehrere ausgewählt sein.
for (int i = 0; i < [selectedItems count] ; i++)
{
[[selectedItems objectAtIndex:i] setColor:currentColor];
}
}
ToDoItem.h :
NSString *category;
NSColor *color;
@property (retain) NSString *category;
@property (retain) NSColor *color;
ToDoItem.m :
@synthesize category;
@synthesize color;
- (id)init {
self = [super init];
if (self) {
color = [NSColor redColor];
}
return self;
}
- (id)initWithCoder:(NSCoder *)decoder
{
if(![super init])
return nil;
[self setCategory:[decoder decodeObjectForKey:@"category"]];
[self setColor:[decoder decodeObjectForKey:@"color"]];
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder
{
[encoder encodeObject:[self category] forKey:@"category"];
[encoder encodeObject:[self color] forKey:@"color"];
}
I hope my code will help you to understand my problem and to get this working.
I look forward to hearing from you. Thanks.