3

我正在使用情节提要和 XCode 4.3 创建一个将标签栏作为根视图的应用程序。即使我的应用程序有其他运行良好的 segue,其中一个也不会触发。它应该在选择选项卡的第三项时触发。我唯一能看到的是标签栏实际上加载了新视图,但没有传递任何信息。

Storyboard 上的连接如下:根视图控制器(选项卡栏)-> 导航控制器-> CapitolDetailViewController(它具有来自另一个视图控制器的推送序列)

我已经在连接、代码等方面尝试了很多更改。但它并没有触发这个 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”是在选择第三项时应该传递信息的那个,而“CapitolDetailViewController”是应该接收它的那个。

我的转场代码:(它甚至没有进入转场,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。看起来标签栏项目和视图控制器没有附加。

4

3 回答 3

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 core data class。如果您需要查看整个项目,请告诉我,并且尝试将一些东西放在一起并放置一个链接以便您可以下载它,但我相信如果您已经创建了一个合适的模型并在应用程序委托中使用了正确的方法,类似于我所做的,它应该工作。希望这对你有帮助,我的朋友。

于 2013-02-07T03:43:08.217 回答
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;
于 2013-02-07T02:51:54.693 回答
0

在我看来,您需要在“SCDMasterViewController”中设置一个名为 managedObjectContext 的属性(我会推荐一个不同的名称)。

错误是说该属性尚未声明。

我希望这有帮助。

于 2013-02-07T02:54:04.203 回答