I’m still new at this, so bear with me:
I created a new Window-based application in XCode 4 that happens to be a universal app. After that, I programmatically created a TabBarController with the associated view controllers (just basic views with 1-2 labels to help me identify each version (iPad, iPhone).
When I run the app and switch tabs, the viewDidLoad method does fire, the title appears correctly, but the visual view does not switch. It remains at the default MainWindow(device).xib file for each version. What am I missing here?
adding the tabs programmatically:
- (void) addTabs
{
//set up a local nav controller which we will reuse for each view controller
UINavigationController *localNavigationController;
//create tab bar controller and array to hold the view controllers
tabBarController = [[UITabBarController alloc] init];
NSMutableArray *localControllersArray = [[NSMutableArray alloc] initWithCapacity:2];
//setup the first view controller (Root view controller)
HomeViewController *myViewController;
myViewController = [[HomeViewController alloc] init];
//create the nav controller and add the root view controller as its first view
localNavigationController = [[UINavigationController alloc] initWithRootViewController:myViewController];
// add the new nav controller (with the root view inside of it)
// to the array of controllers
[localControllersArray addObject:localNavigationController];
//release
[localNavigationController release];
[myViewController release];
//setup the second view controller
MapViewController *secondViewController;
secondViewController = [[MapViewController alloc] initWithTabBar];
localNavigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController];
[localControllersArray addObject:localNavigationController];
[localNavigationController release];
[secondViewController release];
tabBarController.viewControllers = localControllersArray;
[localControllersArray release];
}
and MapViewController.h is basic – there’s really nothing there:
#import "MapViewController.h"
@implementation MapViewController
- (id)initWithTabBar {
if ([self init]) {
//this is the label of the tab button itself
self.title = @"Map";
//image goes here
//self.tabBarItem.image = [UIImage imageNamed:@"name_gray.png"];
//set the long name show in the Navigation bar at the top
self.navigationItem.title = @"Map View";
}
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"Am I loading?");
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(@"Testing");
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
Now, there is no MapViewController.xib file, but the MapViewController_iPhone.xib and the MapViewController_iPad.xib files.
They Should be named:
~iphone, and~ipad, and not: _iPhone and _IPad. The reason for this is that this is the format the reference says it to be.Also note that the Simulator is NOT case sensitive, whereas the iOS device is, so make sure that you have the nib formatted as above, otherwise, it will only work on the Simulator.
Hope that Helps!