ios - Core Data - fetching object -
i have tableviewcontroller food names in it. when click on accessorybutton: - fetches object in row, open detailviewcontroller , pass in init method, string containing name of food object fetched.
the detailviewcontroller let me edit name, , delegate ( detaildidsavetext:(nsstring *) )pass string value tableviewcontroller
the tableviewcontroller in it's delegate implementation wants save value.
if had create new object easy, create new managedobject , call nsentitydescription insertnewentityforname.
but in case need edit object exists. how can that?
how retrieve object editing ?
( know correct way pass object detailvc, i'm doing tests better learn core data, , love try way )
store reference object
@property (nonatomic) food *fooditemtoedit;
and set property when accessory button tapped
- (void)tableview:(uitableview *)tableview accessorybuttontappedforrowwithindexpath:(nsindexpath *)indexpath { self.fooditemtoedit = [self.fetchedresultscontroller objectatindexpath:indexpath]; }
then in delegate method, if objecttoedit exists, create food if doesn't exist, set it's text.
- (void)detaildidsavetext:(nsstring *)text { if (self.fooditemtoedit == nil) { self.fooditemtoedit = [nsentitydescription insertnewobjectforentityforname:@"food" inmanagedobjectcontext:self.context]; } self.fooditemtoedit.text = text; }
Comments
Post a Comment