How can I create a tab view programmatically on iOS
Asked Answered
T

2

5

For an iPhone app how can I create a tab view programmatically, preferably in Objective-C?

Timberlake answered 7/5, 2011 at 9:48 Comment(0)
C
10

It's quite simple to create a UITabBar via the UITabBarController. The following example should work within your AppDelegate class.

App Delegate Interface

Firstly, within the interface, we'll define our UITabBarController.

UITabBarController *tabBarController;

App Delegate Implementation

Then, within the implementation file's application:didFinishLaunchingWithOptions: method, we'll then initialise our tab bar controller.

// Initialise our tab bar controller
UITabBarController *tabBarController = [[UITabBarController alloc] init];

Next, you need to create the view controllers that you want to add to the tab bar controller. We'll need to add some information into these to set the tab's title/icon, but I'll come back to that at the end.

// Create your various view controllers
UIViewController *testVC = [[TestViewController alloc] init];
UIViewController *otherVC = [[OtherViewController alloc] init];
UIViewController *configVC = [[ConfigViewController alloc] init];

As the setViewControllers:animated: method requires an array of view controllers, we'll add our view controllers to an array and then release them. (As the NSarray will retain them.)

// Put them in an array
NSArray *viewControllers = [NSArray arrayWithObjects:testVC, otherVC, configVC, nil];
[testVC release];
[otherVC release];
[configVC release];

Then simply provide the UITabBarController with the array of view controllers and add it to our window.

// Attach them to the tab bar controller
[tabBarController setViewControllers:viewControllers animated:NO];

// Put the tabBarController's view on the window.
[window addSubview:[tabBarController view]];    

Finally, make sure you call [tabBarController release]; within your dealloc method.

View Controller Implementation

Inside each of your view controllers, you'll also want to set the title and icon for the tab within the init method as follows:

// Create our tab bar item
UITabBarItem *tabBarItem = [self tabBarItem];
UIImage *tabBarImage = [UIImage imageNamed:@"YOUR_IMAGE_NAME.png"];
[tabBarItem setImage:tabBarImage];
[tabBarItem setTitle:@"YOUR TITLE"];
Coxswain answered 7/5, 2011 at 10:4 Comment(3)
When I create my own tabBarControllers programmatically, I always create a navigationController for each tab and each navigationController is initialized with a rootViewController. Seems to me that in your example it's not possible to create a navigation stack since you don't have a navigationController, but I never tried coding it like that.Defense
@Wolfgang I suppose it depends on whether or not you'll want to push/pop items to the navigational stack. (My example is a pretty low end "just the basics" approach.)Coxswain
@Timberlake No problems - hope it goes well. :-)Coxswain
D
0

This is how we have to create tabbar programmatically

UINavigationController *BandNavigationController3;
AudienceSettingsViewController *audienceSettingsViewView =[[AudienceSettingsViewController alloc]initWithNibName:@"AudienceSettingsViewController" bundle:nil];
BandNavigationController3 = [[UINavigationController alloc]initWithRootViewController:audienceSettingsViewView];
BandNavigationController3.tabBarItem.title = @"Settings";
BandNavigationController3.tabBarItem.image = [UIImage imageNamed:@"settings.png"];

[BandNavigationController3.tabBarItem initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:4];
BandNavigationController3.navigationBar.hidden = YES;

[bandTabBarArray addObject:BandNavigationController3];
[BandNavigationController3 release];
[audienceSettingsViewView release];

[tabBarController setViewControllers:bandTabBarArray]; 
[bandTabBarArray release];
Delois answered 7/5, 2011 at 9:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.