gpt4 book ai didi

ios - 核心数据 : Set Managed Object Context from AppDelegate for app with UITabBarController

转载 作者:行者123 更新时间:2023-11-29 03:14:06 25 4
gpt4 key购买 nike

我通常使用 AppDelegate.h 中的以下代码为 Root View 设置 managedObjectContext:

UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
HomeViewController *controller = (HomeViewController *)navigationController.topViewController;
controller.managedObjectContext = self.managedObjectContext;

但我当前的应用程序在根目录包含一个自定义 UITabBarController,然后是 UINavigationController,然后是 UIViewController。如何将 managedObjectContext 设置为 View Controller ?

最佳答案

你可以做到非常相似

UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;
// index 0 means left-most tab
UINavigationController *navigationController = (UINavigationController *)tabController.viewControllers[0];
HomeViewController *controller = (HomeViewController *)navigationController.topViewController;
controller.managedObjectContext = self.managedObjectContext;

或者,我更喜欢的是:

在你的 HomeViewController 中做

#import "AppDelegate.h"

// somewhere (e. g. awakeFromNib, loadView or viewDidLoad)
self.managedObjectContext = ((AppDelegate *) [UIApplication sharedApplication].delegate).managedObjectContext

并在您的 AppDelegate 中将 managedObjectContext 设为公共(public)属性。

关于ios - 核心数据 : Set Managed Object Context from AppDelegate for app with UITabBarController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21871646/

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