Question

I am finding it very difficult to grasp how the Views are loaded and wired with the TabBarController in the iPhoneCoreDataRecipes App.

Does anyone have any pointers on how to learn the concepts presented in this Apple sample App? I have read the other items on Apple developer site.

- (void)applicationDidFinishLaunching:(UIApplication *)application {
   recipeListController.managedObjectContext = self.managedObjectContext;
   [window addSubview:tabBarController.view];
   [window makeKeyAndVisible];
}

My understanding of the above
Line 2: Populating the list controller?
Line 3: Adding a Sub view to the tab controller view?

If my understanding of line 3 is correct, where are the other views, Unit Conversion, added to the tabcontroller?

Was it helpful?

Solution

A tab bar controller requires a list of view controllers. I haven't seen this specific sample, so it may be taken care of by the first line. However, normally you would do the following:

  1. Instantiate all view controllers:

    UIViewcontroller *viewControllerTab1 = ...
    UIViewcontroller *viewControllerTab2 = ...
    UIViewcontroller *viewControllerTab3 = ...
    
  2. Add them to the tab bar controller:

    tabBarController.viewControllers = 
      [NSArray arrayWithObjects:
         viewControllerTab1,
         viewControllerTab2,
         viewControllerTab3, 
         nil
      ];
    
  3. Then you would add the tabBarController's view to the window as a subview, followed by window makeKeyAndVisible (lines 2 and 3). These are not specific to the tab bar, they just add the tab bar view to the main application view, like you would any other view controller.

You may also either specify the UITabBarItem details here, or on the view Controllers. You should do it here so that you don't run into any problems with the tab bar items not showing up. I.E.

viewControllerTab1.tabBarItem = [UITabBarItem init...]

OTHER TIPS

Actually:

Line 2: is passing a reference to the managedObjectContext to the recipeListController. A managedObjectContext is a CoreData structure. It includes a reference to the persistent store (often, but not always, a file on disk [SQLite]) and a managedObjectModel (file in the project). This context allows you to make queries against your object model, which has been persisted to the database. (Since this example is recipes: "Fetch all recipes that require the ingredient "Flour"). But since this is not (strictly speaking) a direct database query, the "things" that are returned are objects, not rows in a recordSet. you will only find references to managedObjectContexts in CoreData applications.

Line 3: Has been explained above. This is adding a subview to the application's window, and then assigining the tabBarController.view to that subview. It is assumed that the tabBarController has been previously populated with the appropriate viewControllers.

Hope that helps.

That code smells like the window and the tab controllers are all being loaded in the NIB. Everything is already "wired" up by this point, and all it is doing is setting the window's sub-view and making it key.

This might help. Make a new tab-based application in XCode, and look at what it does. It will have both NIB-loaded and code-defined views in there so you can see the difference.

I think what you are looking for is the same issue I ran across where the view are being loaded in Interface Builder, not the code.

So the steps are:

1: Open mainwindow.xib

2: set the View Mode to column view

what the view mode select looks like

3: open the tab bar controller and navigate down to the tab element

enter image description here

4: set the class of the identity

what the class should look like

I kept trying to connect the main tab itself, but you need to be more specific!

Hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top