views:

52

answers:

1

I've done some reorganizing of my project recently and now I'm not seeing my tab bar controller, but its first view controller's view is appearing. Here's a breakdown of everything that happens prior to the problem.

App Delegate loads FirstViewController with nib. FirstViewController loads the application data from my server and then presents MainViewController with a modal transition. MainViewController is where the UITabBarController is supposed to be appearing. It's a very simple class.

The .h

@interface MainViewController : UIViewController <UITabBarControllerDelegate> {
    IBOutlet UITabBarController *tabBarController;
}

@property (nonatomic, retain) UITabBarController *tabBarController;

@end

The .m

@implementation MainViewController
@synthesize tabBarController;

- (void)viewDidLoad {
NSLog(@"MainViewController viewDidLoad");


//set tab bar controller delegate to self
tabBarController.delegate = self;

// home view
HomeViewController *home = [[HomeViewController alloc] initWithTab];

// menu view 
MenuViewController *menu = [[MenuViewController alloc] initWithTab];

// special offers view 
SpecialOffersViewController *so = [[SpecialOffersViewController alloc] initWithTab];

// events view 
EventsViewController *events = [[EventsViewController alloc] initWithTab];

// info view 
InfoViewController *info = [[InfoViewController alloc] initWithTab];

//populate the tab bar controller with view controllers
NSArray *controllers = [NSArray arrayWithObjects:home, menu, so, events, info, nil];

tabBarController.viewControllers = controllers;

//release view controllers
[home release];
[menu release];
[so release];
[events release];
[info release];
[controllers release];

//add tab bar controller to view
[self.view addSubview:tabBarController.view];

[super viewDidLoad];
}

and here's the bit from FirstViewController that modally presents the MainViewController...

MainViewController *controller = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:controller animated:YES];
    [controller release];

I'm not getting any compiler errors or warnings and the app runs swell... no crashing. It just isn't showing the darned TabBar, and it used to when I was creating it on my AppDelegate. I checked everything in my NIB and my outlets seem to be hooked up ok. I have no idea what's happened. Help!

A: 

I'm not sure if this is the cause of your problem but it might be:

You create your array of controllers, like so:

NSArray *controllers = [NSArray arrayWithObjects:home, menu, so, events, info, nil];

That's fine. controllers has a +0 (autoreleased) retain count.

You then assign it to the tabBar's viewControllers property:

tabBarController.viewControllers = controllers;

That's also fine. controllers now has a +1 retain count (autorelease + retain by tabBarController)

You then release controllers:

[controllers release];

controllers now has a +0 retain count, which means the next time the runloop spins, your array is going to get deallocated, the subsequent views will disappear (because they've been deallocated), and your app will probably end up crashing.

Simple fix: You didn't alloc, retain, or copy the controllers array, so you are not responsible to release it. Remove the release and your memory management will be proper. It might solve your problem, because it's the only thing I immediately see that might be an issue.

Dave DeLong
Good point, but that didn't fix it. What actually did end up working was adding this just before I set the controller's view to the tabBarController's view.CGRect frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);tabBarController.view.frame = frame;I picked it up from Googling around. It doesn't seem right that I should have to do this, but it works.
E-Madd