2
votes

I've looked around online, and haven't been able to find an acceptable solution to this problem...

I'm looking for a simple code pattern:

Load a TabBarController object (with associated subview controllers) from a separate .xib file, instead of including and loading automatically from a default MainWindow.xib.

In XCode terms, starting from a new iPad/iPhone project as a "Tab Bar Application", the goal is to solve the following:

  • Create the project
  • Move: TabBarController, TabBar, FirstViewController, and SelectedSecondViewController from MainWindow.xib, into a new "TabBarController.xib" file
  • After moving, MainWindow.xib should only contain: File's Owner, First Responder, App Delegate, Window
  • In TabBarController.xib, File's Owner and First Responder are set to: UIApplication and UIResponder, respectively.
  • Change "didFinishLaunchingWithOptions" in the main application delegate to the following:

REMOVE:

     [self.window addSubview:tabBarController.view];

ADD:

     UITabBarController *uiTab = [[UITabBarController alloc] initWithNibName:@"TabBarController" bundle:nil]; 
     [self.window addSubview:uiTab.view];

With these changes, the application builds and runs, but when the TabBarController loads the tab bar is "empty" -- there don't appear to be any contents in the controller.

In looking in the debugger, either the "init" isn't initializing from the data correctly, or something in the .xib file is not set correctly.

What is the correct solution to this? I realize there are other ways of doing this, and yes, I have them working in other applications.

What I'm looking for however, is a specific solution using the default project, that can be used as a general pattern for setting up iOS code.

Thanks in advance for any help

  • js
2

2 Answers

2
votes

I think i know what you are looking for because i want the same thing.

  1. Create New Empty xib file at interface builder.
  2. Add to the xib TabBarController from the library.
  3. Edit whatever you need on this tab bar controller on the xib.
  4. Of course, save...

Determine from which view controller do want to create that xib with tab bar controller. In other words, who is the view controller that will cause this tab bar controller to appear. Let's call that view controller ParentViewController

In that view controller, create an IBOutlet to a TabBarController.

Back to the xib, make the identity of the File's Owner to the ParentViewController and of course dont forget to hook up the outlet of the tab bar controller in the file's owner to the tab bar controller in the xib. save the xib and you are ready to go.

When you want to present that tab bar, just decide which way you want to do it: Modally,Popup or something else (Not inside a navigation controller because Apple dont allow tab bar controllers to be inside navigation controllers).

When you decide, just present your tab bar controller outlet the way to present any other view controller. for example:

[self presentModalViewController:self.myTabBarController animated:YES];
1
votes

Assuming you start with the "Tab Bar Application" template and move the UITabBarController and associated view controllers to a new nib as you described...

In your new nib, File's Owner should be set to your AppDelegate class. Then connect the outlet "tabBarController" of File's Owner to the UITabBarController.

Then in your -[application:didFinishLaunchingWithOptions:], do not remove this line:

[self.window addSubview:tabBarController.view];

Instead, load the new nib right before that with your app delegate as the owner:

[[NSBundle mainBundle] loadNibNamed:@"TabBarController" owner:self options:nil];

That will set your tabBarController property (since you made that connection in the nib) and then you can proceed as normal. What you were doing was actually creating a whole new UITabBarController, and not loading the one from the nib at all. (well, ok you were loading it for a brief moment, but then not doing anything useful with it)

Hope that helps.