我有一些视图控制器。在我登录的第一个 LoginViewController 中,我创建了 newEntity。通过单击“登录”按钮,我通过谓词“登录”获取并分配此 newEntity 获取。AppDelegate.h 为 Core Data 创建了一些东西。在 LoginViewController 中,我在 .h 中有下一个字符串:
@class AppDelegate;
...
@property (strong, nonatomic) AppDelegate *appMeDelegate;
@property (strong, nonatomic) NSManagedObjectContext *context;
...
在 LoginViewController.m
#import "AppDelegate.h"
...
@synthesize ... appMeDelegate, context;
...
//I have login button where i compare login and password in
//UITextFields with that in Core Data and if it is
//all right i load fetch for user with his login
-(IBAction)login:
{
appMeDelegate = (AppDelegate *) [[UIApplication sharedApplication]delegate];
context = appMeDelegate.managedObjectContext;
NSFetchRequest * fetchRequest = [[NSFetchRequest alloc]init];
NSPredicate *predicateMe = [NSPredicate predicateWithFormat:@"login==%@",login.text];
fetchRequest.predicate = predicateMe;
... //then I create newEntity }
//next method for saving
-(void)saving
{
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}}
然后在其他 ViewController 我使用 newEntity 但无法保存它
- (void)viewDidLoad
{
[super viewDidLoad];
NSData *newdata = [NSData dataWithData:self.loginViewController.newEntity.photo];
photoArray = [NSMutableArray arrayWithArray:
[NSKeyedUnarchiver unarchiveObjectWithData:newdata]];}
-(IBAction)save
{ [photoArray addObject:myImage];
NSData *newData = [NSKeyedArchiver archivedDataWithRootObject:photoArray];
[self.loginViewController.newEntity setValue:newData forKey:@"photo"];
[self.loginViewController saving];
但没有结果,也没有保存实体的变化