0

我有一些视图控制器。在我登录的第一个 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];

但没有结果,也没有保存实体的变化

4

1 回答 1

1

你会使用这样的东西来保存你的 managedDocument 上下文

    [self.managedDocument saveToURL: self.managedDocument.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:^(BOOL success) {
    if (completionHandler) completionHandler(success);
}];

编辑 对不起,刚刚看到您显然没有使用 NSManagedDocument (您应该查看)。

不要这样做:

[self.loginViewController saving];

像以前一样做:

appMeDelegate = (AppDelegate *) [[UIApplication sharedApplication]delegate];
NSManagedObjectContext *context = appMeDelegate.managedObjectContext;
[context save: &error];
于 2012-12-17T20:43:23.233 回答