1

For a small app, I have a login screen. On Auth, A tab bar controller with two views (one with navigation controller) is presented. I am following this tutorial. It uses core data. http://maybelost.com/2011/12/tutorial-storyboard-app-with-core-data/

enter image description here The tutorial calls a segue. But I would like to use presentModalViewController. It works, except I am wondering how to pass a managedObjectContext to the View inside Navigation Control inside tab bar controller.

I read this Passing ManagedObjectContext to view controllers using storyboards with a root UITabBarController, but the comments under second answer say it is not the right method.

Can someone tell me the correct way to do it? I am looking to know how to get a reference to a view inside the tabbar controller so that I can set the managedobjectcontext for that view.

Thanks

EDIT In appDelegate.h:

@interface AppDelegate : UIResponder <UIApplicationDelegate>
   @property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;

My appDelegate.m :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    LoginViewController *rootView = (LoginViewController *)self.window.rootViewController;
    rootView.managedObjectContext = self.managedObjectContext;
}
Community
  • 1
  • 1
aVC
  • 2,254
  • 2
  • 24
  • 46

1 Answers1

2

Actually what I tend to do is grab the context from the tabBarController of the view you are in. For instance, in your viewWillAppear from the view in the tab bar just create an instance of self.tabBarController and set your views context to that context. That way you can just pass the context to the tabBar and each view can reference that tabBar's context.

Something like this.

MainTabBar *parentTabBarController = (MyTabBar *)self.tabBarController;
self.managedObjectContext = parentTabBarController.managedObjectContext;
Mark McCorkle
  • 9,349
  • 2
  • 32
  • 42
  • is nt it the same as accessing it from the appDelegate? – aVC May 09 '13 at 17:02
  • You can also reference from AppDelegate, yes. The choice is up to you. General practice is to pass – Mark McCorkle May 09 '13 at 17:03
  • Yes, I prefer to pass as well. I updated the question. What confuses me is: rootView is currently the loginviewcontroller's rootView,and tabBar is not yet there. Can you tell me how can I modify your answer, so that after Auth (which is when tabBar is presented), MOC is set for the view inside the first tab's navigation controller? – aVC May 09 '13 at 17:12
  • Are you using different contexts? If not, you can pass the moc at the time of presenting the tabBar view since it's nothing more than a reference to the moc in the appDelegate. If you are passing to a nav controller you can just reference the viewControllers array like UIViewController *theControllerYouWant = [self.navigationController.viewControllers objectAtIndex:(theIndexOfYourViewController)]; – Mark McCorkle May 09 '13 at 18:23
  • right. But can you show how to pass the moc when presenting the tabbar? say I am in loginViewController, which has no navigation bar or tab bar. And after auth, I want to set the MOC in view1 of tabBar tab1. how do I call access this view controller in the modally presented view? I cant use self from loginVC can I? sorry if this is a noob qn. Trying to grasp the basics. – aVC May 09 '13 at 19:52
  • You need to create a segue from your loginViewController to your tabBarController and in the prepareForSegue method compare for that segue identifier then pass the context to the destination Segue. YourTabBarController *tabBar = [segue destinationViewController]; tabBar.managedObjectContext = _managedObjectContext; – Mark McCorkle May 09 '13 at 20:17
  • ok I see. So I 'MUST' create a segue and use prepareforsegue?. Just to clarify, 1. [segue destinationViewController] points to tabbarcontroller or viewcontroller?, 2. Is there a way to do it programattically (without segue)? just like accessing view in navigation controllers with objectAtIndex: method, can we access a tabbar? – aVC May 09 '13 at 20:27
  • Yes, when you create an instance of the tabBar you do the same thing. The prepareForSegue just references the destination view you are placing on the segue. If you are creating and presenting the tabBar manually then just set its context right after you allocate it from your loginViewController. – Mark McCorkle May 09 '13 at 20:29
  • Thanks for trying to be helpful, but I am still having questions. I posted a different question hoping to make it clear. Can you please take a look? http://stackoverflow.com/questions/14143095/storyboard-prepareforsegue – aVC May 09 '13 at 22:00
  • Looks like you got it figured out. ;-) – Mark McCorkle May 09 '13 at 22:51
  • not really. I was asking a question. Because, the code I posted, the tabbarController is not showing :( – aVC May 10 '13 at 01:18
  • Post a picture of the screenshot because in the screenshot on this question there is no segue going to the tabBar. You just need a segue going to the tabBar and then the first view that pops up will be the 1st view inside the tabBar (index 0), the next view would be index 1 and so on. – Mark McCorkle May 10 '13 at 01:26