Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: NSComboBox Coredata bindings
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSComboBox Coredata bindings



Pfou, I think I did it, and I glad to report it to this mailing list, for
comment and future reference...

So I've got the following model (credits to Wain ;) :

< Transaction >
< category > <<------------> < Category >
< category >

Wain was (of course) right : the NSCombobox ' bindings should be set to

content : CategoryArrayController.arrangedObjects
contentValues : CategoryArrayController.arrangedObjects.category
value : TransactionArrayController.selection.category.category

Indeed, value expects a NSString, to be printed in the text area of the
NSCombobox.

Now, Coredata cannot automatically update the 'category' RelationShip in the
Transaction Entity, as the value in not a NSManagedObject.

So we must intercept the NSCombox Selection change, and code it ourselves. I
did it with comboBoxSelectionDidChange: delegate method :

- (void)comboBoxSelectionDidChange:(NSNotification *)notification
{
// User entered a known category name

NSEnumerator *e = [[categoryController arrangedObjects]
objectEnumerator];
id categoryObject;

while ( (categoryObject = [e nextObject]) ) {

if ([[categoryObject valueForKey:@"category"]
isEqualToString:[categoryComboBox objectValueOfSelectedItem]])
{
unsigned int selectionIndex =
[transactionArrayController selectionIndex];

NSManagedObject *transaction =
[[transactionArrayController
arrangedObjects] objectAtIndex:selectionIndex];
[transaction setValue:categoryObject
forKey:@"category"];

break;
}
}
}

Ok, what about adding new category ? I had a little problem, because :

+ nor controlTextDidEndEditing: neither textDidEndEditing: NSComboBox
delegate method were called, for a currently unknown reason
+ an "An error occurred" message modal window appeared, because a category
name was expected...

I had to do something "very early", so I use the NSComboBox
contr:isValidObject: delegate method (from NSControl).

- (BOOL)control:(NSControl *)control isValidObject:(id)object
{
BOOL categoryIsOK = FALSE;
NSManagedObject *category;
NSString *categoryName = [NSString stringWithString:object];
[categoryName retain];

// User entered a known category name
NSEnumerator *e = [[categoryController arrangedObjects]
objectEnumerator];
id categoryObject;

while ( (categoryObject = [e nextObject]) ) {
if ([[categoryObject valueForKey:@"category"]
isEqualToString:categoryName])
{
categoryIsOK = TRUE;
category = categoryObject;
break;
}
}

// Existing category not found, User is creating a new category
if (categoryIsOK == FALSE)
{
NSManagedObjectContext *managedObjectContext = [self
managedObjectContext];

category = [NSEntityDescription
insertNewObjectForEntityForName:@"Category"
inManagedObjectContext:managedObjectContext];

[category setValue:categoryName forKey:@"category"];
[categoryController addObject:category];
categoryIsOK = TRUE;
}

unsigned int selectionIndex = [transactionArrayController
selectionIndex];

NSManagedObject *transaction = [[transactionArrayController
arrangedObjects] objectAtIndex:selectionIndex];

[transaction setValue:category forKey:@"category"];

return TRUE;
}


I'd be glad to hear comments on the above code. That currently seems to work
all right.

Julien
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Re: NSComboBox Coredata bindings (From: Julien Bordet <email@hidden>)
 >Re: NSComboBox Coredata bindings (From: Wain <email@hidden>)
 >Re: NSComboBox Coredata bindings (From: Julien Bordet <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.