Answer the question
In order to leave comments, you need to log in
How to implement relationship in Core Data?
Good afternoon. I am currently working on an application that has multiple screens. So, in order:
1. On the first screen there are several buttons (English, Spanish, Chinese), when pressed, they go to the second screen. Suppose you clicked on English.
2. On the second screen, the usual table view with a list of lessons that the user has created. Also in the nav bar is the "Add" button to add a new lesson.
3. When clicking on a cell in the table, it goes to the next screen, where there is a collection of cards implemented using UIScrollView (in the process of implementation). There is also an add button at the top of the nav bar to add a new card.
Attention question: how to make sure that the card is added specifically for this lesson? At the moment, it turns out that when I add it, it is added to all lessons, but I need this one specifically.
Table view code for displaying the list of lessons (EnglishTablewViewController.m)
@implementation EnglishTableViewController
-(NSManagedObjectContext *)managedObjectContext{
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]){
context = [delegate managedObjectContext];
}
return context;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"EnglishCourse"];
self.cources = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
[self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.cources count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EnglishCell" forIndexPath:indexPath];
NSManagedObject *course = [self.cources objectAtIndex:indexPath.row];
[cell.textLabel setText:[NSString stringWithFormat:@"%@", [course valueForKey:@"name"]]];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSManagedObjectContext *context = [self managedObjectContext];
if (editingStyle == UITableViewCellEditingStyleDelete){
[context deleteObject:[self.cources objectAtIndex:indexPath.row]];
NSError *error = nil;
if(![context save:&error]){
NSLog(@"Не удалилось! %@ %@", error, [error localizedDescription]);
return;
}
[self.cources removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
@implementation AddEnglishCourseViewController
-(NSManagedObjectContext *)managedObjectContext{
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]){
context = [delegate managedObjectContext];
}
return context;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)save:(id)sender {
NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"EnglishCourse" inManagedObjectContext:context];
[newDevice setValue:self.nameTextField.text forKey:@"name"];
NSError *error = nil;
if(![context save:&error]){
NSLog(@"Can't save! %@ %@", error, [error localizedDescription]);
}
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)cancel:(id)sender {
[self dismissViewControllerAnimated:NO completion:nil];
}
@end
Answer the question
In order to leave comments, you need to log in
If I understood correctly, then you plan to add 2 controller views for each language (list and add)? If so, then you have problems in the architecture.
It makes sense to single out work with CoreData in a singleton. And add a "language" field to each element, which will contain which language this card belongs to.
And depending on the selected language, your controllers will only show elements that have the required language in the "language" field.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question