i have method in viewcontroller.m called getdata called inside viewdidload:
-(void)getdata { appdelegate *appdelegate = [[uiapplication sharedapplication] delegate]; nsmanagedobjectcontext *context = [appdelegate managedobjectcontext]; nsentitydescription *entitydesc = [nsentitydescription entityforname:@"workouthasexercise" inmanagedobjectcontext:context]; nsfetchrequest *request = [[nsfetchrequest alloc] init]; [request setentity:entitydesc]; request.resulttype = nsdictionaryresulttype; request.propertiestofetch = [nsarray arraywithobjects:@"exercisename", @"reps", @"sets", nil]; nspredicate *pred = [nspredicate predicatewithformat:@"(workoutname = %@)", _workoutname]; [request setpredicate:pred]; nsmanagedobject *matches = nil; nserror *error; nsarray *objects = [context executefetchrequest:request error:&error]; if ([objects count] == 0) { } else { [_exercises removeallobjects]; (int x = 0; x < [objects count]; x++) { matches = objects[x]; [_exercises addobject:[matches valueforkey:@"exercisename"]]; [_totalsets addobject:[matches valueforkey:@"sets"]]; [_totalreps addobject:[matches valueforkey:@"reps"]]; [_currentsets addobject:[nsnumber numberwithinteger:0]]; } } [_exercisestableview reloaddata]; }
i have custom uitableviewcell 2 buttons initiated in cellforrowatindexpath:
activeworkoutcell *cell = (activeworkoutcell *)[tableview dequeuereusablecellwithidentifier:simpletableidentifier]; nsarray *nib = [[nsbundle mainbundle] loadnibnamed:@"activeworkoutcell" owner:self options:nil]; cell = [nib objectatindex:0]; cell.increasebutton.tag = indexpath.row; cell.decreasebutton.tag = indexpath.row;
in activeworkoutcell.m have 2 ibactions buttons:
- (ibaction)decreasesets:(id)sender { activeworkoutviewcontroller *vc = [[activeworkoutviewcontroller alloc] init]; [vc decreasesets:[sender tag]]; } - (ibaction)increasesets:(id)sender { activeworkoutviewcontroller *vc = [[activeworkoutviewcontroller alloc] init]; [vc increasesets:[sender tag]]; }
the ibactions call these 2 methods in viewcontroller.m
-(void)increasesets:(nsinteger)row { [self getdata]; //there code here increase value of currentsets[row] } -(void)decreasesets:(nsinteger)row { [self getdata] //code decrease value... }
problem:
when getdata called viewdidload, works fine. problem occurs when returning viewcontroller.m ibaction in activeworkoutcell.m. when call [self getdata] in increasesets fetch request returns empty array. confusing me - code works fine when first called not @ when called second time after custom cell action has been triggered.
here viewdidload if helps:
- (void)viewdidload { [super viewdidload]; _exercises = [nsmutablearray array]; _totalsets = [nsmutablearray array]; _currentsets = [nsmutablearray array]; _totalreps = [nsmutablearray array]; _titlelabel.text = _workoutname; _exercisestableview.allowsselection = no; [self getdata]; }
_workoutname given value in prepareforsegue in previous view controller.
i think found issue. instantiating "activityworkoutviewcontroller" when ibaction methods called , new instance , in methods calling [self getdata] pointing new instance has no variables instantiated or viewdidload happened, mutable arrays not allocated , hence empty.
just use old instance of class data.
i not sure how referencing classes. in confusion that. but, might check allocations , calling right class data