尝试检查不起作用的 segue 方法。确保情节提要中 segue 的标识符与班级中的标识符一致。从您的代码和所有注释的外观来看,您的代码似乎很好并且应该可以工作。唯一可能给您带来问题的地方是故事板中 segue 的不同标识符。我希望这能解决你的问题,我的朋友。
编辑:
好吧,我不知道这是否适合您,但我在标签栏应用程序中设置核心数据有不同的方法。在委托的 .h 文件中,几乎所有内容都是通用的,特别是在 .m 中,applicationdidfinishwithoption
我使用索引号来对添加到选项卡栏的视图控制器中的视图进行排序。我希望这会有所帮助,这是我使用的代码。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self setupFetchedResultsController];
if (![[self.fetchedResultsController fetchedObjects] count] > 0 ) {
NSLog(@"!!!!! ~~> There's nothing in the database so defaults will be inserted");
[self importCoreDataDefaultRoles];
}
else {
NSLog(@"There's stuff in the database so skipping the import of default data");
}
// The Tab Bar
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
// The Two Navigation Controllers attached to the Tab Bar (At Tab Bar Indexes 0 and 1)
UINavigationController *view1 = [[tabBarController viewControllers] objectAtIndex:0];
UINavigationController *view2 = [[tabBarController viewControllers] objectAtIndex:1];
View1 *view1 = [[view1nav viewControllers] objectAtIndex:0];
personsTVC.managedObjectContext = self.managedObjectContext;
View2 *view2 = [[view2nav viewControllers] objectAtIndex:0];
rolesTVC.managedObjectContext = self.managedObjectContext;
return YES;
}
通过使用索引号,我能够在没有任何错误的情况下实现相同的场景,并且对于启动视图,我使用了一个作为初始模式视图插入的子视图。希望这可以帮助你。就我而言,核心数据要添加到多个选项卡中,我不知道您是否有相同的需求。
编辑 1
这是我的Appdelegate.h
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
@end
这是app delegate.m
#import "AppDelegate.h"
#import "PersonsTVC.h"
#import "RolesTVC.h"
@implementation AppDelegate
@synthesize window = _window;
@synthesize managedObjectContext = __managedObjectContext;
@synthesize managedObjectModel = __managedObjectModel;
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator;
@synthesize fetchedResultsController = __fetchedResultsController;
- (void)insertRoleWithRoleName:(NSString *)roleName
{
Role *role = [NSEntityDescription insertNewObjectForEntityForName:@"Role"
inManagedObjectContext:self.managedObjectContext];
role.name = roleName;
[self.managedObjectContext save:nil];
}
- (void)importCoreDataDefaultRoles {
NSLog(@"Importing Core Data Default Values for Roles...");
[self insertRoleWithRoleName:@"Player 1"];
[self insertRoleWithRoleName:@"Player 2"];
[self insertRoleWithRoleName:@"Player 3"];
[self insertRoleWithRoleName:@"Player 4"];
[self insertRoleWithRoleName:@"Player 5"];
[self insertRoleWithRoleName:@"Player 6"];
[self insertRoleWithRoleName:@"Player 7"];
[self insertRoleWithRoleName:@"Player 8"];
[self insertRoleWithRoleName:@"Player 9"];
[self insertRoleWithRoleName:@"Player 10"];
[self insertRoleWithRoleName:@"Player 11"];
[self insertRoleWithRoleName:@"Player 12"];
NSLog(@"Importing Core Data Default Values for Roles Completed!");
}
- (void)setupFetchedResultsController
{
// 1 - Decide what Entity you want
NSString *entityName = @"Role"; // Put your entity name here
NSLog(@"Setting up a Fetched Results Controller for the Entity named %@", entityName);
// 2 - Request that Entity
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName];
// 3 - Filter it if you want
//request.predicate = [NSPredicate predicateWithFormat:@"Person.name = Blah"];
// 4 - Sort it if you want
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name"
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)]];
// 5 - Fetch it
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
[self.fetchedResultsController performFetch:nil];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self setupFetchedResultsController];
if (![[self.fetchedResultsController fetchedObjects] count] > 0 ) {
NSLog(@"!!!!! ~~> There's nothing in the database so defaults will be inserted");
[self importCoreDataDefaultRoles];
}
else {
NSLog(@"There's stuff in the database so skipping the import of default data");
}
// The Tab Bar
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
// The Two Navigation Controllers attached to the Tab Bar (At Tab Bar Indexes 0 and 1)
UINavigationController *personsTVCnav = [[tabBarController viewControllers] objectAtIndex:0];
UINavigationController *rolesTVCnav = [[tabBarController viewControllers] objectAtIndex:1];
// The Persons Table View Controller (First Nav Controller Index 0)
PersonsTVC *personsTVC = [[personsTVCnav viewControllers] objectAtIndex:0];
personsTVC.managedObjectContext = self.managedObjectContext;
// The Roles Table View Controller (Second Nav Controller Index 0)
RolesTVC *rolesTVC = [[rolesTVCnav viewControllers] objectAtIndex:0];
rolesTVC.managedObjectContext = self.managedObjectContext;
//NOTE: Be very careful to change these indexes if you change the tab order
// Override point for customization after application launch.
// UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
// RolesTVC *controller = (RolesTVC *)navigationController.topViewController;
// controller.managedObjectContext = self.managedObjectContext;
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
}
- (void)applicationWillTerminate:(UIApplication *)application
{
[self saveContext];
}
- (void)saveContext
{
NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil)
{
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error])
{
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}
#pragma mark - Core Data stack
- (NSManagedObjectContext *)managedObjectContext
{
if (__managedObjectContext != nil)
{
return __managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil)
{
__managedObjectContext = [[NSManagedObjectContext alloc] init];
[__managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return __managedObjectContext;
}
- (NSManagedObjectModel *)managedObjectModel
{
if (__managedObjectModel != nil)
{
return __managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"momd"];
__managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return __managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (__persistentStoreCoordinator != nil)
{
return __persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"StaffManager.sqlite"];
NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return __persistentStoreCoordinator;
}
#pragma mark - Application's Documents directory
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
@end
现在请记住,我将这两个类都用于数据实体,并在这两个类中声明了托管对象。可以肯定的是,在使用核心数据时,您的初始视图必须具有托管对象。如果这对您没有帮助,那么我建议您可以查看 tim roadley 的核心数据教程或 paul hagurty 的 stanford core data class。如果您需要查看整个项目,请告诉我,并且尝试将一些东西放在一起并放置一个链接以便您可以下载它,但我相信如果您已经创建了一个合适的模型并在应用程序委托中使用了正确的方法,类似于我所做的,它应该工作。希望这对你有帮助,我的朋友。