我正在使用storyboard和XCode 4.3来创建一个以根视图显示标签栏的应用。即使我的应用程序有其他工作正常的segue,其中一个不会触发。它应该在选择选项卡的第三项时触发。我唯一能看到的是标签栏实际上加载了新视图,但没有传递任何信息。
故事板上的连接如下:Root View控制器(标签栏) - >导航控制器 - > CapitolDetailViewController(具有来自另一个视图控制器的推送segue)
我在连接,代码等方面做了很多改动但是它只是没有触发这个segue。我试图更改AppDelegate中的代码以添加缺少的新控制器。我的applicationDidFinishLaunching的代码如下:
NSManagedObjectContext *context = [self managedObjectContext];
NSError *error;
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
tabBarController = (UITabBarController *)self.window.rootViewController;
UINavigationController *view1 = [[tabBarController viewControllers] objectAtIndex:0];
UINavigationController *view2 = [[tabBarController viewControllers] objectAtIndex:1];
UINavigationController *view3 = [[tabBarController viewControllers] objectAtIndex:2];
SCDMasterViewController *view11 = [[view1 viewControllers] objectAtIndex:0];
view11.managedObjectContext = self.managedObjectContext;
SerieDetailViewController *view22 = [[view2 viewControllers] objectAtIndex:0];
CapitolDetailViewController *view33 = [[view3 viewControllers] objectAtIndex:0];
return YES;
其中“SerieDetailViewController”是应该在选择第3项时传递信息的那个,而“CapitolDetailViewController”是应该接收它的那个。
我的segue的代码:(它甚至没有进入segue,de NSLOG从未出现过)
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSLog(@"Entra segue");
if ([segue.identifier isEqualToString:@"mostraCapitol"]) {
NSString *nom = @"";
nom= serieName;
CapitolDetailViewController *destViewController = segue.destinationViewController;
destViewController.serieName2 = nom;
}
}
任何人都知道出了什么问题?
更新:即使我收到了帮助我纠正某些编译错误的答案,但问题仍然存在。如果单击选项卡栏项,则会加载空视图。它从不称为segue。看起来没有附加标签栏项和视图控制器。
答案 0 :(得分:2)
尝试检查不起作用的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核心数据类。如果你需要一个整个项目来看看让我知道和生病尝试得到一些东西并放置一个链接,以便你可以下载它,但我相信如果你已经创建了一个合适的模型,并在app委托使用正确的方法类似于我做了什么,它应该工作。希望这可以帮助你我的朋友。
答案 1 :(得分:0)
查看此示例iPhone代码可能对您有所帮助
http://developer.apple.com/library/ios/#samplecode/iPhoneCoreDataRecipes/Introduction/Intro.html
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get a reference to our detail view
PicturelistDetail *pld = (PicturelistDetail *)[segue destinationViewController];
// Pass the managed object context to the destination view controller
pld.managedObjectContext = managedObjectContext;
// If we are editing we need to pass some stuff, so check the segue title first
if ([[segue identifier] isEqualToString:@"EditPicture"])
{
// Get the row we selected to view
NSInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row];
// Pass the object from the table that we want to view
pld.currentPicture = [pictureListData objectAtIndex:selectedIndex];
}
}
请将其更改为您的代码并尝试使用。
在视图控制器中声明属性
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
答案 2 :(得分:0)
在我看来你需要在'SCDMasterViewController'中设置一个名为managedObjectContext的属性(我建议使用不同的名称)。
错误是说该属性尚未声明。
我希望这会有所帮助。