Passing a managed entity context using a tab controller

Well, I tried to figure it out again and again.

I know that it’s best practice for the application delegate to pass the managed object context to the first view controller in the application, and then each subsequent view controller to pass the managed object context down. However, when I use the tab bar controller in my application, I may seem to be enveloping this extra layer.

The only way I was able to figure out how to do this was to get the root controller on each "Go back" tab in the application delegate in order to capture the context, but as I understand it, this is a bad form.

+3
source share
4 answers

The key, after all, did not rely on the interface designer to create the tab bar controller. By doing this manually in code, I can easily pass the managed object context to the view controller, as I create them in applicationatoinDidFinishLaunchingWithOptions:

I used this article as a basis: http://www.iphonelife.co.uk/creating-a-uitabbarcontroller-programmatically/

+1
source

You can use the interface builder to achieve the same.

The following is a small modification (for some extra clarity) of the original Rog proposal - pay attention to

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 in the implementation file:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
       vc1.managedObjectContext = self.managedObjectContext;
       vc2.managedObjectContext = self.managedObjectContext;
       vc3.managedObjectContext = self.managedObjectContext;
       // Continue with your implementation logic
}

Interface Builder ctrl App Delegate View, , View , .

+2

- AppDelegate:

CoreDataUsingViewController *vc = (CoreDataUsingViewController *)[[tabBarController viewControllers] objectAtIndex:1];
vc.managedObjectContext = self.managedObjectContext;

coreData UITabBarControllers . , , .

.

How to use ManagedObjectContext when using UITabBarController

+1
source

Not sure if I understand your problem correctly, but why not just pass the MOC to other view controllers in the same way? Here is an example:

@interface AppDelegate : NSObject <UIApplicationDelegate> {
    ViewController1 *vc1;
    ViewController2 *vc2;
    ViewController3 *vc3;
}

// Declare properties as per normal

Then in the implementation file:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
       vc1.managedObjectContext = self.managedObjectContext;
       vc2.managedObjectContext = self.managedObjectContext;
       vc3.managedObjectContext = self.managedObjectContext;
       // Continue with your implementation logic
}

Hope this helps! Horn

0
source

Source: https://habr.com/ru/post/1791869/


All Articles