移交ManagedObjectContext的问题 [英] Issues handing over managedObjectContext

查看:84
本文介绍了移交ManagedObjectContext的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读有关此项目的大多数问题,但不知何故我无法弄清楚。

I have been reading up most questions on this item, but somehow I can't figure this one out.

我设法轻松地将托管项目移交给了AppDelegate到我尝试过的先前应用程序中的第一个视图,其中初始viewController也处理数据。

I managed easily to hand over the ManagedObjectContext from the AppDelegate to the first view in previous applications I tried, where the initial viewController handled also the data.

现在在我正在构建的新应用程序中,我需要一个初始屏幕用户需要进行选择(通过一些可能的按钮)。在此屏幕上,根据故障排除,我的MOC仍然完整(NSLog消息为我提供了该MOC的有效地址)。

Now in a new application I am building, I need an initial screen where the user needs to make a selection (through a few possible buttons). On this screen my MOC is still intact as per my troubleshooting (the NSLog message gives me a valid address for the MOC).

选择完成后,该应用进入带有TableView的UIViewController。在此视图中,我应该能够管理数据,但我的MOC会返回nil。

Once the selection has been made, the app proceeds to a UIViewController with a TableView in it. In this view I should be able to manage my data, but my MOC returns nil.

应用程序通过UINavigationController进入下一个视图。

The app proceeds to the next view via a UINavigationController.

我在AppDelegate中合并了以下代码:

I incorporated following code in my AppDelegate:

UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
InitialViewController *controller = (InitialViewController *)navigationController.topViewController;
controller.managedObjectContext = self.managedObjectContext;
return YES;

InitialViewController是第一个ViewController,现在一切正常。

InitialViewController is the FIRST ViewController, where all is still fine.

下一个视图控制器称为MasterViewController,它是NSFetchedControllerDelegate。在这里,我似乎无法访问NOC。我已经尝试过在同一主题的先前问题中提出的几种方法和解决方案,但似乎都没有用。

The next viewcontroller is called MasterViewController and is the NSFetchedControllerDelegate. Here I can't seem to get access to the NOC. I have tried several approaches and solutions suggested in previous questions on the same topic, but none seem to work.

如何通过适当的编码才能使NOC在

How can I with proper coding get the NOC to work in the MasterViewController?

哦,是的,一种有效的解决方案是在MasterViewController中的ViewDidLoad之后,我插入以下代码:

Oh yeah, one of the solutions that worked is after the ViewDidLoad in the MasterViewController, I insert the following code:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
self.managedObjectContext = appDelegate.managedObjectContext;

我的MOC现在可用,但这是正确的方法吗?

My MOC is available now, but is this the right way to do it?

推荐答案

如果使用情节提要,

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    [super prepareForSegue:segue sender:sender];
    if ([[segue identifier] isEqualToString:@"segueNameOfPresentingYourModalViewController"] == YES) {
        UINavigationController *navigationController = segue.destinationViewController;
        YourCustomViewController *aController = (YourCustomViewController *)navigationController.topViewController;
        aController.managedObjectContext = self.managedObjectContext;
    }
    else if ([segue.identifier isEqualToString:@"segueNameOfPushingYourViewController"] == YES) {
        YourCustomViewController *aController = segue.destinationViewController;
        aController.managedObjectContext = self.managedObjectContext;
    }
}

如果您不使用情节提要,请同样进行设置查看控制器的managedObjectContext,您将在其中推送或展示它。另一个常见的地方是 -tableView:didSelectRowAtIndexPath:方法。

If you are not using Storyboards, you similarly set your view controller's managedObjectContext where you are about to push or present it. Another common place is -tableView:didSelectRowAtIndexPath: method.

这篇关于移交ManagedObjectContext的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆