感谢上面的帖子,这很棒,我设法对其进行了排序。这是我的代码,可能很笨拙,但它确实有效。
首先,当我将 fetchedObjects 加载到 SelectorView 控制器中的 PickerView 中时,我使用以下代码将属性“isSelectedPractice”设置为“NO”:
for (Practice *fetchedPractice in [self.fetchedResultsController fetchedObjects]) {
[fetchedPractice setValue:@"NO" forKey:@"isSelectedPractice"];
[self.managedObjectContext save:nil];
然后,我确定了选定的实践:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
Practice *practice = [[self.fetchedResultsController fetchedObjects] objectAtIndex:row];
self.selectedPractice = practice;
NSLog(@"The '%@' practice was selected using the picker", self.selectedPractice.name);
}
当视图 Segue 回到 RegularViewController 时,我将 selectedPractice 的 isSelectedPractice 属性设置为 YES。我保留它这么晚,因为我不希望 PickerView 中的多个选择导致多个对象的 isSelectedPractice YES。
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"SavedPractice Segue"])
{
[self.selectedPractice setValue:@"YES" forKey:@"isSelectedPractice"];
[self.managedObjectContext save:nil];
NSLog(@"Setting SelectedPractice as '%@' in RegularViewController with isSelectedPractice as '%@'",self.selectedPractice.name,self.selectedPractice.isSelectedPractice );
RegularViewController *rvc= segue.destinationViewController;
rvc.delegate = self;
rvc.selectedPractice = self.selectedPractice;
}
else {
NSLog(@"Unidentified Segue Attempted!");
}
}
然后我在 RegularViewController 的 setupFetchedResultsController 方法中设置以下 Predicate:
request.predicate = [NSPredicate predicateWithFormat:@"isSelectedPractice = %@", @"YES"];
非常感谢您的帮助