本文介绍了核心数据详细信息视图与关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我已在我的核心数据实体上设置了一对多关系。我试图显示相关数据的detailview副本。目前我有prepareforseague:方法使用原始实体(Routines),但我失去了如何显示链接实体(RoutinesDetails)。 FBCDRoutineViewController - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated ]; //从持久性数据存储中获取设备 NSManagedObjectContext * managedObjectContext = [self managedObjectContext]; NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@Routines]; self.routines = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy]; [self.tableView reloadData]; } - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if [[segue identifier] isEqualToString:@ UpdateDevice]){ NSManagedObject * selectedDevice = [self.routines objectAtIndex:[[self.tableView indexPathForSelectedRow] row]]; FBCDRoutineViewController * destViewController = segue.destinationViewController; destViewController.routines = selectedDevice; } FBCDRoutineDetailViewController - (NSManagedObjectContext *)managedObjectContext { NSManagedObjectContext * context = nil; id delegate = [[UIApplication sharedApplication] delegate]; if([delegate performSelector:@selector(managedObjectContext)]){ context = [delegate managedObjectContext]; } return context; } - (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; if(self){ //自定义初始化} return self; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; //从持久性数据存储中获取设备 NSManagedObjectContext * managedObjectContext = [self managedObjectContext]; NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@RoutinesDetails]; self.routines = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy]; } - (void)viewDidLoad { [[self navigationController] setNavigationBarHidden:NO animated:YES]; [super viewDidLoad]; //在加载视图之后执行任何其他设置。 if(self.routines){ [self.testLabel setText:[self.routines valueForKey:@routinename]]; } } FBCDRoutineDetailViewController @property(strong)NSManagedObject * routines; 这是我第一次使用核心数据,我正在查看如何显示Details实体。我接近得到它的工作?如果不是,我可以得到指向我应该看什么。 有任何建议吗?解决方案您要显示与 prepareForSegue 中传递的 Routines 对象相关的所有对象。 然后在 FBCDRoutineDetailViewController 中声明两个属性: @property(strong)NSManagedObject * routines; //传递的对象 @property(strong)NSManagedObject * routineDetails; //显示的细节对象 并按如下方式获取: NSManagedObjectContext * managedObjectContext = [self managedObjectContext]; NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@RoutinesDetails]; NSPredicate * predicate = [NSPredicate predicateWithFormat:@routineinfo ==%@,self.routines]; [fetchRequest setPredicate:predicate]; NSError * error; self.routineDetails = [managedObjectContext executeFetchRequest:fetchRequest error:& error]; self.routineDetails 现在是数据源 (注意:为了在表视图中显示Core Data请求的结果集,,您可能还会考虑使用 NSFetchedResultsController 。) I have set up a 1 to many relationship on my core data entities. I am trying to show the detailview copy of the associated data. Currently I have the prepareforseague: method working with the original entity(Routines), however I am at a lose as to how to show the linked entity (RoutinesDetails).FBCDRoutineViewController - (void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; // Fetch the devices from persistent data store NSManagedObjectContext *managedObjectContext = [self managedObjectContext]; NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Routines"]; self.routines = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy]; [self.tableView reloadData];} - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString:@"UpdateDevice"]) { NSManagedObject *selectedDevice = [self.routines objectAtIndex:[[self.tableView indexPathForSelectedRow] row]]; FBCDRoutineViewController *destViewController = segue.destinationViewController; destViewController.routines = selectedDevice; }FBCDRoutineDetailViewController- (NSManagedObjectContext *)managedObjectContext { NSManagedObjectContext *context = nil; id delegate = [[UIApplication sharedApplication] delegate]; if ([delegate performSelector:@selector(managedObjectContext)]) { context = [delegate managedObjectContext]; } return context;}- (id)initWithStyle:(UITableViewStyle)style{ self = [super initWithStyle:style]; if (self) { // Custom initialization } return self;}- (void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; // Fetch the devices from persistent data store NSManagedObjectContext *managedObjectContext = [self managedObjectContext]; NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"RoutinesDetails"]; self.routines = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];}- (void)viewDidLoad{ [[self navigationController] setNavigationBarHidden:NO animated:YES]; [super viewDidLoad]; // Do any additional setup after loading the view. if (self.routines) { [self.testLabel setText:[self.routines valueForKey:@"routinename"]]; }}FBCDRoutineDetailViewController@property (strong) NSManagedObject *routines;This is my first time with core data and I am looking at how to show the Details entity. Am I Close to getting it working? If not can I get directed at to what I should be looking at.Any suggestions? 解决方案 If I understand your problem correctly, you want to display all RoutinesDetails objectsthat are related to the Routines object passed in prepareForSegue.Then you would declare two properties in the FBCDRoutineDetailViewController:@property (strong) NSManagedObject *routines; // the passed object@property (strong) NSManagedObject *routineDetails; // the displayed details objectsand fetch them like this:NSManagedObjectContext *managedObjectContext = [self managedObjectContext];NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"RoutinesDetails"];NSPredicate *predicate = [NSPredicate predicateWithFormat:@"routineinfo == %@", self.routines];[fetchRequest setPredicate:predicate];NSError *error;self.routineDetails = [managedObjectContext executeFetchRequest:fetchRequest error:&error];self.routineDetails is now the data source array for the details view.(Remark: For displaying the result set of a Core Data request in a table view,you might also consider to use a NSFetchedResultsController.) 这篇关于核心数据详细信息视图与关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 02:13