iphone - 如何在 RootViewController 之外的另一个 View 上加载核心数据?

标签 iphone cocoa-touch core-data ios4 uiviewcontroller

除了 AppDelegate 中的代码之外,我基本上拥有核心数据和应用程序正常工作。我遇到问题的代码如下:

- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    RootViewController *tableController = [[RootViewController alloc] initWithStyle:UITableViewStylePlain];
    tableController.managedObjectContext = [self managedObjectContext];

    self.navigationController = [[UINavigationController alloc] initWithRootViewController:tableController];
    [tableController release];

    [window addSubview: [self.navigationController view]];
    [window makeKeyAndVisible];

}

我不想在启动时将 ManagedObjectContext 设置为 Root View Controller 。我想让它成为另一个 View Controller 。但是,如果我将类更改为我需要的 View Controller ,它会在启动应用程序时加载该 View Controller ,这不是我想要做的。我仍然想启动 Root View ,但我希望能够加载其他 View Controller 的核心数据上下文。我真的很困惑如何解决这个问题。到目前为止,我已经花了两天时间试图找到解决此问题的方法,但还没有成功。任何帮助将不胜感激。

另外,如果我在 appdelegate didfinishlaunching 中省略以下内容:

RootViewController *tableController = [[RootViewController alloc] initWithStyle:UITableViewStylePlain];
        tableController.managedObjectContext = [self managedObjectContext];

        self.navigationController = [[UINavigationController alloc] initWithRootViewController:tableController];
        [tableController release];

我收到此错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'Hello'

编辑: 这是实体代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"Lap Times";

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addTime:)];
    self.navigationItem.rightBarButtonItem = addButton;
    [addButton release];

    [self fetchRecords];

}

- (void)addTime:(id)sender {

    addTimeEvent *event = (addTimeEvent *)[NSEntityDescription insertNewObjectForEntityForName:@"addTime" inManagedObjectContext:self.managedObjectContext];
    [event setTimeStamp: [NSDate date]];

    NSError *error;
    if (![managedObjectContext save:&error]) {
        // This is a serious error saying the record could not be saved.
        // Advise the user to restart the application
    }

    [eventArray insertObject:event atIndex:0];
    [self.tableView reloadData];

}

- (void)fetchRecords {

    // Define our table/entity to use
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"addTime" inManagedObjectContext:self.managedObjectContext];

    // Setup the fetch request
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entity];

    // Define how we will sort the records
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];

    [request setSortDescriptors:sortDescriptors];
    [sortDescriptor release];

    // Fetch the records and handle an error
    NSError *error;
    NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];

    if (!mutableFetchResults) {
        // Handle the error.
        // This is a serious error and should advise the user to restart the application
    }

    // Save our fetched data to an array
    [self setEventArray: mutableFetchResults];

    [mutableFetchResults release];
    [request release];

}

此外,如果我使用自己的名为 MyAppDelegate 的应用程序委托(delegate)

MyAppDelegate *tableController = [[MyAppDelegate alloc] initWithStyle:UITableViewStylePlain];
tableController.managedObjectContext = [self managedObjectContext];

self.navigationController = [[UINavigationController alloc] initWithRootViewController:tableController];

我收到以下错误:

Object cannot be set- either readonly property or no setter found

最佳答案

我看不出您原来采用的方法有什么问题?您基本上是在应用程序委托(delegate)中创建 managedObjectContext 并通过赋值将其传递给 tableController

另一种方法是让你的 viewController 从 App 委托(delegate)中“请求” ManagedObjectContext 。因此,您仍然需要将 CoreData 方法放置在 AppDelegate 中,并在您想要获取对上下文的引用时使用以下方法。由于 ManagedObjectContext 是根据请求延迟加载的,因此只有在您第一次访问应用委托(delegate)中的 managedObjectContext 方法时,它才会被实例化。

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

PS1:显然您需要将 AppDelegate 替换为您的应用程序的正确名称。

PS2:进行更改时出现错误的原因是没有可供 CoreData 使用的上下文。

关于iphone - 如何在 RootViewController 之外的另一个 View 上加载核心数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4982627/

相关文章:

iphone - iOS - 从 App Store 安装旧版本的应用程序

iphone - 在 Storyboard 中创建 TabBarControllerDelegate

ios - 核心数据与 NSFetchedResultsController :Invalid update with with a property in fetchObjects

core-data - 核心数据获取...将托管对象插入上下文 A 并保存上下文 A 后,为什么不使用上下文 B 获取托管对象?

javascript - 在 Android 浏览器的 Javascript 中获取 iframe 内容的高度

javascript - 带有叠加层的 Titanium.Media.showCamera,通过事件

iphone - iOS 6 - 状态保存和恢复

iOS - 在选项卡更改时运行代码

ios - 为什么applicationFrame和statusBarFrame是从两个不同的对象获取的?

ios - 如何在 swift 中使用 fetchedresultscontroller 重新排序 tableview 行?