gpt4 book ai didi

ios - 使用 prepare for segue 将托管对象上下文从 ViewController 传递到 TarBarViewController

转载 作者:行者123 更新时间:2023-11-28 20:38:24 25 4
gpt4 key购买 nike

我正在尝试从 ViewController 传递核心数据托管对象上下文(它能够将相同内容从 AppDelegate 加载到 TabBarViewController。

AppDelegate.m



<pre><code>// Pass the managed object context to the root view controller (the login view)
LoginViewController *rootViewController = (LoginViewController *)self.window.rootViewController;
rootViewController.managedObjectContext = self.managedObjectContext;
</code></pre>

<p>LoginViewController.m</p>

<pre><code>- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"LoginSegue"]){
UITabBarController *tabBarViewController = (UITabBarController *) [segue destinationViewController];
ReceiptsListViewController *receiptsListViewController = (ReceiptsListViewController *) [[tabBarViewController viewControllers] objectAtIndex:5];
receiptsListViewController.managedObjectContext = self.managedObjectContext;
}
}
</code></pre>

<p>When i run it i encounter this error </p>

2012-03-09 01:47:03.723 Peets[22893:fb03] -[UINavigationController setManagedObjectContext:]: unrecognized selector sent to instance 0x6d91900
2012-03-09 01:47:03.747 Peets[22893:fb03] *** WebKit discarded an uncaught exception in the webView:shouldInsertText:replacingDOMRange:givenAction: delegate: <NSInvalidArgumentException> -[UINavigationController setManagedObjectContext:]: unrecognized selector sent to instance 0x6d91900

最佳答案

我不明白您想如何将 MOC 从 VC 传递到 TBVC。通常 RootViewController 是 TBVC。如果这也是你的情况,你可以像这样通过 MOC:

#import "AppDelegate.h"
#import "MasterViewController.h"

@implementation AppDelegate

@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;

UINavigationController *navigationController = (UINavigationController *)[[tabController viewControllers] objectAtIndex:0];
MasterViewController *controller = (MasterViewController *)[[navigationController viewControllers] objectAtIndex:0];
controller.managedObjectContext = self.managedObjectContext;

navigationController = (UINavigationController *)[[tabController viewControllers] objectAtIndex:1];
controller = (MasterViewController *)[[navigationController viewControllers] objectAtIndex:0];
controller.managedObjectContext = self.managedObjectContext;

navigationController = (UINavigationController *)[[tabController viewControllers] objectAtIndex:2];
controller = (MasterViewController *)[[navigationController viewControllers] objectAtIndex:0];
controller.managedObjectContext = self.managedObjectContext;

return YES;
}

顺便说一句:此示例基于 Xcode 中的主从应用程序模板。默认情况下它没有 TBVC,但如果您像屏幕截图中那样添加 TBVC,上面的代码将起作用。

enter image description here

关于ios - 使用 prepare for segue 将托管对象上下文从 ViewController 传递到 TarBarViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9624333/

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