gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 20:16:20 25 4
gpt4 key购买 nike

除了 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com