Okay, I've tried to figure this out over and over again.
好吧,我已经试过一遍又一遍的解决这个问题。
I know the best practice is to have the App Delegate pass the managed object context to the first view controller in an application, and then have each subsequent view controller pass the managed object context down. However, when I'm using a Tab Bar Controller in my application, I can seem to wrap my head around that extra layer.
我知道最佳实践是让App委托将托管对象上下文传递给应用程序中的第一个视图控制器,然后让每个后续的视图控制器将托管对象上下文传递下去。然而,当我在我的应用程序中使用标签栏控制器时,我似乎可以把我的头绕在额外的层上。
The only way I've been able to figure out how to do it is have the root view controller of each tab "Reach Back" into the app delegate to grab the context, but as I understand it this is poor form.
我能弄明白的唯一方法是让每个选项卡的根视图控制器“返回”到app委托中去获取上下文,但据我所知,这是一个糟糕的表单。
4 个解决方案
#1
2
You can use interface builder to achieve the same thing.
您可以使用接口构建器实现相同的功能。
Here is a slightly modified (for some additional clarity) version of Rog's original suggestion - notice the IBOutlet's
这里是Rog最初建议的一个稍作修改(为了增加一些清晰度)的版本——请注意IBOutlet的版本
@interface AppDelegate : NSObject <UIApplicationDelegate> {
ViewController1 *vc1;
ViewController2 *vc2;
ViewController3 *vc3;
}
@property (nonatomic, retain) IBOutlet ViewController1 *vc1;
@property (nonatomic, retain) IBOutlet ViewController2 *vc2;
@property (nonatomic, retain) IBOutlet ViewController3 *vc2;
Then on the implementation file:
然后对实现文件:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
vc1.managedObjectContext = self.managedObjectContext;
vc2.managedObjectContext = self.managedObjectContext;
vc3.managedObjectContext = self.managedObjectContext;
// Continue with your implementation logic
}
Then from within Interface Builder ctrl drag from your App Delegate to the View Controller nested within the Tab Bar Controller and hook up the relevant View controller from the contextual menu that appears.
然后,从接口构建器内部,从App委托ctrl拖动到嵌套在选项卡栏控制器中的视图控制器,并从上下文菜单中连接相关的视图控制器。
#2
1
The key was, in the end, not to rely on interface builder to build the tab bar controller. By doing it manually in code I'm able to easily pass the managed object context to the view controller as I create them in applicatoinDidFinishLaunchingWithOptions:
最后,关键是不要依赖接口构建器来构建选项卡栏控制器。通过手工编写代码,当我在applicatoinDidFinishLaunchingWithOptions中创建时,我可以很容易地将托管对象上下文传递给视图控制器:
I used this article as my basis: http://www.iphonelife.co.uk/creating-a-uitabbarcontroller-programmatically/
我使用本文作为基础:http://www.iphonelife.co.uk/creating-a-uitabbarcontroller-programmatically/
#3
1
You can also just do something like this in your AppDelegate:
你也可以在AppDelegate中这样做:
CoreDataUsingViewController *vc = (CoreDataUsingViewController *)[[tabBarController viewControllers] objectAtIndex:1];
vc.managedObjectContext = self.managedObjectContext;
I was adding coreData to an existing project with a few different build targets and didn't want to recreate all the different UITabBarControllers from scratch. It was pretty easy to do this way, though I'm not sure if it's the most artful way to do it or not.
我在一个现有的项目中添加了coreData,它有几个不同的构建目标,并不想从头开始创建所有不同的uitabbarcontroller。这样做很容易,尽管我不确定这是否是最巧妙的方法。
See also
How to share a ManagedObjectContext when using UITabBarController
如何在使用UITabBarController时共享ManagedObjectContext
#4
0
Not sure if I understand your issue correctly but why not simply pass the MOC to the other view controllers in the same manner? Here's an example:
不知道我是否正确理解了您的问题,但为什么不以同样的方式将MOC传递给其他视图控制器呢?这里有一个例子:
@interface AppDelegate : NSObject <UIApplicationDelegate> {
ViewController1 *vc1;
ViewController2 *vc2;
ViewController3 *vc3;
}
// Declare properties as per normal
Then on the implementation file:
然后对实现文件:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
vc1.managedObjectContext = self.managedObjectContext;
vc2.managedObjectContext = self.managedObjectContext;
vc3.managedObjectContext = self.managedObjectContext;
// Continue with your implementation logic
}
I hope it helps! Rog
我希望它可以帮助!罗格
#1
2
You can use interface builder to achieve the same thing.
您可以使用接口构建器实现相同的功能。
Here is a slightly modified (for some additional clarity) version of Rog's original suggestion - notice the IBOutlet's
这里是Rog最初建议的一个稍作修改(为了增加一些清晰度)的版本——请注意IBOutlet的版本
@interface AppDelegate : NSObject <UIApplicationDelegate> {
ViewController1 *vc1;
ViewController2 *vc2;
ViewController3 *vc3;
}
@property (nonatomic, retain) IBOutlet ViewController1 *vc1;
@property (nonatomic, retain) IBOutlet ViewController2 *vc2;
@property (nonatomic, retain) IBOutlet ViewController3 *vc2;
Then on the implementation file:
然后对实现文件:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
vc1.managedObjectContext = self.managedObjectContext;
vc2.managedObjectContext = self.managedObjectContext;
vc3.managedObjectContext = self.managedObjectContext;
// Continue with your implementation logic
}
Then from within Interface Builder ctrl drag from your App Delegate to the View Controller nested within the Tab Bar Controller and hook up the relevant View controller from the contextual menu that appears.
然后,从接口构建器内部,从App委托ctrl拖动到嵌套在选项卡栏控制器中的视图控制器,并从上下文菜单中连接相关的视图控制器。
#2
1
The key was, in the end, not to rely on interface builder to build the tab bar controller. By doing it manually in code I'm able to easily pass the managed object context to the view controller as I create them in applicatoinDidFinishLaunchingWithOptions:
最后,关键是不要依赖接口构建器来构建选项卡栏控制器。通过手工编写代码,当我在applicatoinDidFinishLaunchingWithOptions中创建时,我可以很容易地将托管对象上下文传递给视图控制器:
I used this article as my basis: http://www.iphonelife.co.uk/creating-a-uitabbarcontroller-programmatically/
我使用本文作为基础:http://www.iphonelife.co.uk/creating-a-uitabbarcontroller-programmatically/
#3
1
You can also just do something like this in your AppDelegate:
你也可以在AppDelegate中这样做:
CoreDataUsingViewController *vc = (CoreDataUsingViewController *)[[tabBarController viewControllers] objectAtIndex:1];
vc.managedObjectContext = self.managedObjectContext;
I was adding coreData to an existing project with a few different build targets and didn't want to recreate all the different UITabBarControllers from scratch. It was pretty easy to do this way, though I'm not sure if it's the most artful way to do it or not.
我在一个现有的项目中添加了coreData,它有几个不同的构建目标,并不想从头开始创建所有不同的uitabbarcontroller。这样做很容易,尽管我不确定这是否是最巧妙的方法。
See also
How to share a ManagedObjectContext when using UITabBarController
如何在使用UITabBarController时共享ManagedObjectContext
#4
0
Not sure if I understand your issue correctly but why not simply pass the MOC to the other view controllers in the same manner? Here's an example:
不知道我是否正确理解了您的问题,但为什么不以同样的方式将MOC传递给其他视图控制器呢?这里有一个例子:
@interface AppDelegate : NSObject <UIApplicationDelegate> {
ViewController1 *vc1;
ViewController2 *vc2;
ViewController3 *vc3;
}
// Declare properties as per normal
Then on the implementation file:
然后对实现文件:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
vc1.managedObjectContext = self.managedObjectContext;
vc2.managedObjectContext = self.managedObjectContext;
vc3.managedObjectContext = self.managedObjectContext;
// Continue with your implementation logic
}
I hope it helps! Rog
我希望它可以帮助!罗格