how to create a tabbar programmatically and adding

2020-02-10 10:45发布

问题:

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...

回答1:

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.



回答2:

You have to keep a reference to the tab bar controller. For instance you might keep it in the App Delegate...