gpt4 book ai didi

ios - Segue 不会在标签栏应用程序中触发

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:16:12 26 4
gpt4 key购买 nike

我正在使用 Storyboard 和 XCode 4.3 创建一个将选项卡栏作为 Root View 的应用程序。尽管我的应用程序有其他运行良好的转场,但其中一个转场不会触发。它应该在选择选项卡的第三项时触发。我唯一能看到的是标签栏实际上加载了新 View ,但没有传递任何信息。

Storyboard 上的连接如下:Root View controller (Tab bar) -> Navigation Controller -> CapitolDetailViewController(它有来自另一个 View Controller 的 push segue)

我已经在连接、代码等方面尝试了很多更改。但是它并没有触发这个 segue。我试图更改我的 AppDelegate 中的代码以添加缺少的新 Controller 。我的 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;

}
}

有谁知道是怎么回事吗?

更新:尽管我收到了帮助我更正一些编译错误的答案,但问题仍然存在。如果我单击选项卡栏项目,它会加载一个空 View 。它从不调用 segue。看起来标签栏项目和 View Controller 没有附加。

最佳答案

尝试检查不起作用的 segue 方法。确保 Storyboard中 segue 的标识符与类(class)中的标识符一致。从您的代码和所有注释的外观来看,您的代码似乎很好并且应该可以工作。唯一可能给您带来问题的地方是 Storyboard中 segue 的不同标识符。我希望这能解决您的问题,我的 friend 。

编辑:

好吧,我不知道这是否适合你,但我有不同的方法来设置标签栏应用程序中的核心数据。在委托(delegate)的 .h 文件中,几乎所有内容都是通用的,在 .m 中,特别是 applicationdidfinishwithoption 我使用索引号对添加到选项卡栏的 View Controller 中的 View 进行排序。我希望这会有所帮助,这是我使用的代码。

- (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;
}

通过使用索引号,我能够在没有任何错误的情况下实现相同的场景,并且对于启动 View ,我使用了一个我插入的 subview 作为初始模态视图。希望这对你有帮助。在我的例子中,核心数据被添加到多个选项卡,我不知道你是否有同样的需求。

编辑 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

现在请记住,我将这两个类都用于数据实体,并在两个类中声明了托管对象。在使用核心数据时,可以肯定的是,您的初始 View 必须具有托管对象。如果这对您没有帮助,那么我建议您可以看看蒂姆·罗德利的核心数据教程或保罗·哈格蒂的斯坦福核心数据类(class)。如果您需要查看整个项目,请告诉我,我会尝试将一些东西放在一起并放置一个链接以便您可以下载它,但我相信如果您已经创建了一个合适的模型并且在应用程序委托(delegate)中使用了类似于以下内容的正确方法我所做的,它应该工作。希望这对你有帮助,我的 friend 。

关于ios - Segue 不会在标签栏应用程序中触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14742514/

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