Tryk ⌘ - N. Opret en ny UIViewController med en XIB opkaldt AddIngredientViewController. Dobbeltklik på IngredientsRootViewController.h og skriv:
class Opskrifter;
interface SelectFromIngredientsViewController: UITableViewController {
Opskrifter * opskrifter,
NSManagedObjectContext * managedObjectContext;
NSFetchedResultsController * fetchedResultsController;
}
property (nonatomic, bevarer) Opskrifter * opskrifter,
property (nonatomic, beholde) NSManagedObjectContext * managedObjectContext;
property (nonatomic, beholde) NSFetchedResultsController * fetchedResultsController;
end
Åbn IngredientsRootViewController.
m og skriv:
#import "IngredientsRootViewController.h"
#import "Ingredient.h"
#import "AddIngredientViewController.h"
#import "CoreDataTutorialPart2AppDelegate.h"
implementation IngredientsRootViewController
synthesize managedObjectContext, fetchedResultsController;
- (void) viewDidLoad {
[super viewDidLoad]
UIBarButtonItem * addButton = [[ ,,,0],UIBarButtonItem Alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemAdd mål: self handling:selector (addIngredient)];
self.navigationItem.
leftBarButtonItem = addButton;
[addButton frigivelse]
//Afkommentér følgende linje til at vise en Edit knappen i navigationslinjen for denne visning controller
self.navigationItem.rightBarButtonItem = self.editButtonItem;.
CoreDataTutorialPart2AppDelegate * appDelegate = (CoreDataTutorialPart2AppDelegate *) [ ,,,0],[UIApplication sharedApplication] delegeret]
self.managedObjectContext = appDelegate.
managedObjectContext;
NSError * fejl = intet;
if ([[selv fetchedResultsController] performFetch:! & fejl ;]) {
NSLog (@ "Uløste fejl% @,% @", fejl, [fejl UserInfo]),
abort ();
}
}
- (void) addIngredient {
AddIngredientViewController * addIngredientView = [[AddIngredientViewController Alloc] initWithNibName: @ "AddIngredientViewController" bundt: [NSBundle mainBundle]];
< p> Ingrediens * ingrediens = [NSEntityDescription insertNewObjectForEntityForName: @ "ingrediens" inManagedObjectContext: self.
managedObjectContext];
addIngredientView.ingredient = ingrediens
UINavigationController * navController = [[UINavigationController Alloc] initWithRootViewController: