I am making an IPhone app and I have a table view that is pulling data from Core Data using an frc controller and it displays stuff sorted by title. If the user navigates to the next screen they can click one of three buttons that should change the sort descriptor in the frc. How can I call that frc on the next screen and change it’s sort descriptor based on the button the user presses?
- (IBAction)btnValue:(id)sender {
}
- (NSFetchedResultsController *)frc_Work
{
// If the frc is already configured, simply return it
if (_frc_Work) return _frc_gradedWork;
// Otherwise, create a new frc, and set it as the property (and return it below)
_frc_Work = [_cdStack frcWithEntityNamed:@"GradedWork" withPredicateFormat:nil predicateObject:nil sortDescriptors:@"title,YES" andSectionNameKeyPath:nil];
return _frc_Work;
}
Use delegate or Notifications to do the communication between the screens whenever a button in next screen is pressed.
on notification keep the sort descriptor as a property. Change it when u recive the notification and set a dirty flag variable whose value you will check in viewWillAppear to reload the page.
Detailed Explanation:
You have Two screens (I am assuming two view controllers)
First Screen shows sorted data based on certain criteria.
while querying core data , you need to pass this NSSortdescriptor to the request to fetch the right data.
This screen will have an NSSortDescriptot property.
Second screen shows three buttons, have a common IBAction for them.
based on the Button pressed call a delegate method say SortingCriteriaChanged:(NSSortDescriptor *sortDescriptor),
Create and passthe appropriate sort descriptor to this delegate method.
Before pushing this second screen, set the first screen as delegate to this screen and also implement the above method.
In the delegate method set the sortDescriptor to the one passed to delegate method.
Now dont reload anything.
When you come back to the previous screen, viewWillAppear gets called. here you should reload the sorted list that too only if sorting criteria has chnaged.
For this you need a BOOL property say: needToReload.
set it to YES in delegate method and check its value in viewWillAppear and reload the list.
after reloading of list is done, set the BOOL property to NO again.