views:

462

answers:

3

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?

+1  A: 

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 = ...
  1. Add them to the tab bar controller:
tabBarController.viewControllers = [NSArray arrayWithObjects:

viewControllerTab1, viewControllerTab2, viewControllerTab3, nil];

  1. 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...]

Hector Ramos
You are right about adding an array of viewcontrollers to the tabcontroller, that is what I had seen in previous samples but this code does not exist in this sample. Leading me to believe as Nick mentions, the wiring is done in the XIB, how do I inspect the wiring in the XIB would be my followup I guess?
Picflight
I haven't had any luck working with XIB wired tab controllers. I understand you would just drag and drop more tab items into the tab bar controller, and link them to their view controllers. I rather programatically define the view controller array.
Hector Ramos
A: 

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.

Nick Veys
+1  A: 

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.

mmc