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

kubap007

macrumors newbie
Original poster
Jul 21, 2008
9
0
Hello!

I've some problem.
I've 3 entities: BillOfSale, Product, Item.

When I remove an instance of an Item entity I want to decrement total field (attribiute in BillOfSale entity). I wrote a custom class: ItemMO where I increment total fileld using reletionship:

Code:
[self setValue: sum forKeyPath:@"billOfSale.total"];

I call this function when I change quantity or product and it's working.
But I don't know what method is called when an instance of an entity is being removed.
In the interface I bind "Delete Item" button to ItemsController (NSArayController for Item entity) remove action.

I'm looking for something like awakeFromInsert method but I can't find it.

Please help me :(
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
I assume the object will always be removed in the UI, then I would create a custom remove method which checked the sum value manually. Then I'd call that method instead of remove on the NSArrayController.

You'll probably then have to remove the object manually from the NSManagedObjectContext using [context deleteObject:obj]; where obj is the object in question.
 

kubap007

macrumors newbie
Original poster
Jul 21, 2008
9
0
OK thanks I will try but I don't know how to bind button to method defined by me. Earlier I just copy method implementation from data model and changed it so it wasn't a problem.
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
I assume you have a custom controller... You just control drag from the button to your custom controller.
 

kubap007

macrumors newbie
Original poster
Jul 21, 2008
9
0
Unfortunately I haven't, I wrote custom classes and select them for chosen entity in data model. Obviously I have 3 controllers one for each entity, but I drag them from pallet and I change only controlled entity. Sorry for my stupid questions but I just started with cocoa and I must learn a lot.

I was searching how to create custom controller and I probably know how. I should create NSArrayController subclass, but I still don't know how to override remove method . I create files for new subclass and write method - (void)removeObject:(id)object but I don't know how I can change total field or even delete object. When I override for example method -(void)setQuantity: (short)value I used keyword self and path forKeyPath:mad:"billOfSale.total"
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
You don't want to subclass NSArrayController. You will basically always need a standard Cocoa controller for something (especially once you do anything complex), and this is something you'll need one for.

If you want to check out the idea you could take a look at my source code.
 

kubap007

macrumors newbie
Original poster
Jul 21, 2008
9
0
Thanks a lot I will see your code and I hope I will understand :)

How can I get object form (id)sender.

- (void) remove: (id)sender
{
NSManagedObject *obj = [??? sender];
...
}

Or do I have to do something else?
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
^^ My code may be hard to understand as its pretty advanced, but it is Core Data.

To get the object you want to do

NSManagedObject *obj=[[arrayController selectedObjects] objectAtIndex:0];

Where arrayControlller is an IBOutlet from the custom controller (in the header file at the top section) to the NSArrayController in Interface builder.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.