how to create a tabbar programmatically and adding buttons on it

24,682

Since a tab bar controller is a container view controller that you use to divide your application into two or more distinct modes of operation, most apps have navigation controllers as children of tab bar controllers.

Apple's position is this:

You use tab bar controllers in situations where your application either presents different types of data or presents the same data in significantly different ways.

That is not to say you cannot do things differently... The main question you have is that you have already placed a Nav Controller in the app and you want to create the tab bar controller programmatically. The only way I can therefore see this is that you don't mind if the tabbar controller changes each time you change screens within the Nav Controller. Some apps work this way. Most do not.

If my assumptions above are true I would suggest you rethink your code to see if you want to pursue this line of development. If so, you can easily create a tabbar controller and attach it within the current view.

Here is code I use to create my setup for one of my apps:

// 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
UITabBarController *tabBarController = [[UITabBarController alloc] init];

NSMutableArray *localControllersArray = [[NSMutableArray alloc] initWithCapacity:1];

// setup the first view controller (Root view controller)
RootViewController *myViewController;
myViewController = [[RootViewController alloc] initWithTabBar];

// create the nav controller and add the root view controller as its first view
localNavigationController = [[UINavigationController alloc] initWithRootViewController:myViewController];
localNavigationController.navigationBar.barStyle = UIBarStyleBlack;
localNavigationController.delegate = self;

[localControllersArray addObject:localNavigationController];

// release since we are done with this for now
[localNavigationController release];
[myViewController release];

tabBarController.viewControllers = localControllersArray;
tabBarController.moreNavigationController.navigationBar.barStyle = UIBarStyleBlack;  

tabBarController.delegate = self;
tabBarController.moreNavigationController.delegate = self;

// release the array because the tab bar controller now has it
[localControllersArray release];

self.tabBarController.selectedIndex = 0;

// add the tabBarController as a subview in the window
[window addSubview:tabBarController.view];

// need this last line to display the window (and tab bar controller)
[window makeKeyAndVisible];

There are so many situations where I feel it is easier to do everything programatically.

Hope this helps.

Share:
24,682

Related videos on Youtube

Linux world
Author by

Linux world

enered in to linux scripting ... working on SAN based...

Updated on July 11, 2020

Comments

  • Linux world
    Linux world almost 4 years

    i want to add buttons on to my tab bar programmatically in my view...

    i am having navigation controller but it does not allow me to add these .. i want to create programmatically in my view...