ここでコアデータのチュートリアルに従っています。RootViewcontroller と addRecipeViewController があります。
迷子にならないように、いくつかのクラスと関数、およびフローの画面を以下にリストします。
Recipe.h
#import <CoreData/CoreData.h>
@interface Recipes : NSManagedObject
{
}
@property (nonatomic, retain) NSString * recipeName;
@property (nonatomic, retain) NSString * cookingTime;
@end
addRecipeViewController.h
@class Recipes;
@interface AddRecipeViewController : UIViewController <UITextFieldDelegate> {
Recipes *recipes;
UITextField *textFieldOne;
UITextField *textFieldTwo;
}
addRecipeViewController.m
- (void)save {
1.recipes.recipeName = textFieldOne.text;
2.recipes.cookingTime = textFieldTwo.text;
3.NSError *error = nil;
4.if (![recipes.managedObjectContext save:&error]) {
// Handle error
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}
[self dismissModalViewControllerAnimated:YES];
}
RootViewController.m
- (void)insertNewObject {
AddRecipeViewController *addRecipeView = [[AddRecipeViewController alloc] initWithNibName:@"AddRecipeViewController" bundle:[NSBundle mainBundle]];
Recipes *recipes = (Recipes *)[NSEntityDescription insertNewObjectForEntityForName:@"Recipes" inManagedObjectContext:self.managedObjectContext];
addRecipeView.recipes = recipes;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController: addRecipeView];
[self.navigationController presentModalViewController:navController animated:YES];
[addRecipeView release];
}
流れの絵:
Save
addRecipeViewController のイベントをクリックすると、 に保存recipes
され managedObjectContext
ます。遅かれ早かれ、rootViewConroller は以下を使用してデータを取得しますmanagedObjectContext
。NSFetchedResultsController
QUESTIONmanageObjectContext
:すべてのビュー コントローラーでどのように同じであるmanageObjectContext
かrootViewController
がRecipe
わかりませんmanageObjectContext
。addRecipeViewController
この問題を理解するのを手伝ってください。
ここではすべてのコメントを歓迎します。