Iphone 编辑后将实体保存在核心数据中

Iphone 编辑后将实体保存在核心数据中,iphone,objective-c,ios,core-data,Iphone,Objective C,Ios,Core Data,我有一些视图控制器。在我登录的第一个LoginViewController中,我创建了新实体。通过单击“Login”按钮,我通过谓词“Login”为这个新实体进行获取和分配。h已经为核心数据创建了一些东西。在LoginViewController中,我在.h中有下一个字符串: @class AppDelegate; ... @property (strong, nonatomic) AppDelegate *appMeDelegate; @property (strong, nonatomic)

我有一些视图控制器。在我登录的第一个LoginViewController中,我创建了新实体。通过单击“Login”按钮,我通过谓词“Login”为这个新实体进行获取和分配。h已经为核心数据创建了一些东西。在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];

但没有结果,也没有保存实体中的更改

您可以使用类似的方式保存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];

是的,我不使用NSManagedDocument,我使用调试器,并且我看到我的方法-(void)saving没有被调用=(为什么?它通过这个字符串[self.loginViewController saving],但不转到方法进行保存。在我需要写这个字符串的地方,你写我(appMeDelegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];NSManagedObjectContext*context=appMeDelegate.managedObjectContext;[context save:&error];)?很可能是因为未正确设置.loginViewController引用。您应该通过应用程序委托获取对上下文的引用,并使用我在“其他ViewController”保存方法中编写的内容(如我所说,而不是[self.loginViewController保存];”)我发现您还可以通过访问对loginViewController的引用来访问要更改的实体。我认为这就是问题所在,因为这显然没有正确设置。在其他ViewController保存方法中,请检查self.loginViewController是否为nil。我想它是nil或其属性。如果我理解正确,newEntity是nilYes。例如:进一步的具体问题开启了一个新的话题,因为讨论时间太长了