xcode 在 iOS 中传递上下文以将 Core Data 与 Storyboard 一起使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8275126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:33:18  来源:igfitidea点击:

Passing context in iOS to use Core Data with Storyboard

iosxcodecore-datamanagedobjectcontext

提问by yassassin

I'm having some problems in passing context from the app delegate to the view controller. I've found many tutorials on the internet, and all suggest to use the didFinishLaunchingWithOptionsmethod to create the view controller, set the context property and push it. My problem is that I want to use storyboard, and the view controller is created and pushed within it, and not in the app delegate.

我在将上下文从应用程序委托传递到视图控制器时遇到了一些问题。我在网上找了很多教程,都建议使用该didFinishLaunchingWithOptions方法创建视图控制器,设置上下文属性并推送它。我的问题是我想使用故事板,并在其中创建并推送视图控制器,而不是在应用程序委托中。

I've tried to do this in my app delegate:

我尝试在我的应用程序委托中执行此操作:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//instantiate local context
NSManagedObjectContext *context = [self managedObjectContext];
if (!context)
{
    // Handle the error.
    NSLog(@"Error: Context is null");
}

//reference the view controller
helloCoreDataViewController1_001 *rootViewController = [helloCoreDataViewController1_001 alloc];

// Pass the managed object context to the view controller
rootViewController.managedObjectContext = context;

return YES;
}

and this in my view controller:

这在我的视图控制器中:

@implementation helloCoreDataViewController1_001

@synthesize name, address, phone, status, managedObjectContext;
//....

- (IBAction)saveContact
{
NSLog(@"name: %@",self.name.text);
NSLog(@"address: %@",self.address.text);
NSLog(@"phone: %@",self.phone.text); 

//Save the new instance of the contact entity
Contact *contact = (Contact *)[NSEntityDescription insertNewObjectForEntityForName:@"Contacts" inManagedObjectContext:managedObjectContext];

[contact setContactName:[NSString stringWithFormat:@"%@",self.name.text]];
[contact setValue:self.address.text forKey:@"address"];
[contact setContactPhone:[NSString stringWithFormat:@"%@",self.phone.text]];

NSError *error = nil;

if (![managedObjectContext save:&error])
{
    // Handle the error.
    NSLog(@"error: %@", error.description);
    self.status.text = @"Error: contact NOT saved";
}
else
    self.status.text = @"Contact saved";
}

When I debug, I can see that in the app delegate, the context is populated correctly, and also the property in the view controller is ok. But when my saveContactmethod is invoked, the context is empty.

当我调试时,我可以看到在应用程序委托中,上下文已正确填充,并且视图控制器中的属性也正常。但是当我的saveContact方法被调用时,上下文是空的。

Do you have any suggestions about this? How can I pass the context to the view controller with storyboard?

你对此有什么建议吗?如何使用情节提要将上下文传递给视图控制器?

采纳答案by Caroline

You can get the Storyboard's rootviewcontroller in didFinishLaunchingWithOptions by accessing

您可以通过访问在 didFinishLaunchingWithOptions 中获取 Storyboard 的 rootviewcontroller

self.window.rootViewController

instead of allocing a new one.

而不是分配一个新的。

So those two lines in your didFinishLaunchingWithOptions should look like this:

所以你 didFinishLaunchingWithOptions 中的这两行应该是这样的:

helloCoreDataViewController1_001 *rootViewController = (helloCoreDataViewController1_001 *)self.window.rootViewController;
rootViewController.managedObjectContext = context;

回答by Farini

Instead of passing your managed obeject context to the view controller try to get it on the view controller from the appDelegate:

不要将您的托管对象上下文传递给视图控制器,而是尝试从 appDelegate 将其获取到视图控制器上:

- (NSFetchedResultsController *)fetchedResultsController
{
    if (fetchedResultsController != nil) {
        return fetchedResultsController;
    }
if (managedObjectContext == nil) {
    id appDelegate = (id)[[UIApplication sharedApplication] delegate]; 
    self.managedObjectContext = [appDelegate managedObjectContext];
    }... //finish your fetch request of course...
}

With me it worked beautifully

和我一起工作得很好