3

Still new to iOS programming, and despite copious amounts of research, I have run in to another roadblock.

What I want to implement:

I want a UITabBarController that gets loaded when I navigate from the main UI. I would also like to use a NIB to define its properties.

All of the examples I can find put the UITabBarController in the AppDelegate, but I would not like to load it unless it gets used. I also dont know if all of the UIGestureRecognizers would remain active if I just did it modally (I cant get a working implementation).

What I have so far

First, I load an initial loading view from AppDelegate

AppDelegate.h

@class InitialViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIViewController *viewController;

@end

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[InitialViewController alloc] initWithNibName:@"InitialViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}

From this view, as I am just making a skeleton of the UI, I have two buttons, one goes to what would be the main interface, and the other to the UITabBarController.

InitialViewController.h

@interface InitialViewController : UIViewController
- (IBAction)toMain:(id)sender;
- (IBAction)toTabs:(id)sender;

@property (strong, nonatomic) UIViewController *mviewController;
@property (strong, nonatomic) UIViewController *tviewController;
@end

InitialViewController.m

- (IBAction)toMain:(id)sender {

self.mviewController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
[[[UIApplication sharedApplication] delegate] window].rootViewController = self.mviewController;

}

- (IBAction)toTabs:(id)sender {
self.tviewController = [[tabViewController alloc] initWithNibName:@"tabViewController" bundle:nil];

[[[UIApplication sharedApplication] delegate] window].rootViewController = self.tviewController;

}

On loading MainViewController, it behaves exactly like I want. But when I load the tab view, I get one long tab at the bottom and a black background. I can add in things in viewdidload, like changing the background color, but no actual tabs or views linked to the tabs in the XIB.

I suspect there is something I am missing in two areas: in the tab .h, and some linking associated with that in interface builder. Or setting a new rootViewController isnt enough.

tabBarController.h

#import <UIKit/UIKit.h>

@interface iPodViewController : UITabBarController <UITabBarControllerDelegate>
@end

If someone can point me in the right direction and/or show me an implementation that works, I would be most grateful.

-- as a note, when I go in to the tabbar.xib, and use the assistant editor, it opens InitialViewController.h --

solenoid
  • 954
  • 1
  • 9
  • 20
  • I found a solution, but, I don't understand one part of the functionality. The first problem was I was subclassing `UITabbarController`, so I made a new `UIViewController` to load the tabs from. now, in `- (void)viewDidLoad`, I get the tabs from: ` thetabs = [[UITabBarController alloc] initWithNibName:@"tabcontrol" bundle:nil]; [[NSBundle mainBundle] loadNibNamed:@"tabcontrol" owner:self options:nil]; window = [[[UIApplication sharedApplication] delegate] window]; window.rootViewController = thetabs; ` but I do not know why `initWithNibName` does nothing and the `NSBundle` does. – solenoid Jan 24 '12 at 15:14

1 Answers1

3

Unlike other view controllers (e.g. UITableViewController) you should not subclass the UITabViewController. Therefore, unlike you other view controllers, you don't subclass and then make your subclass the owner of the nib, pointing at the view in the nib, with a customised view.

Instead, for whichever class that you want to own your UITabBarController, add a plain, vanilla UITabBarController as an outlet property on this class. (e.g. your app delegate).

Then create a nib file and drag a UITabBarController object into the nib. Set the owner of the nib to be the class that you want to own your tab bar controller (e.g. your app delegate) and connect the outlet you created as a property to the tab bar controller in the nib.

@interface myTabOwningClass

    @property (strong, nonatomic) IBOutlet UITabBarController myTabBarControllerOutlet;

Now at the point you want to create and display your tab bar controller, use the following method:

[[NSBundle mainBundle] loadNibNamed:@"MyTabControllerNib" owner:myTabOwningClass options:nil];

This will initialise the property (i.e. myTabBarControllerOutlet in our example) on the owning class and load the tab bar controller from the nib, including all sub view controllers for each tab etc. that you have defined in the nib.

gamozzii
  • 3,911
  • 1
  • 30
  • 34