How to programmatically create a tab and add buttons to it

I want to add buttons to the tab bar programmatically ...

I have a navigation controller, but it does not allow me to add them. I want to create programmatically, in my opinion ...

+3
source share
2 answers

Because the tab bar controller is a container view controller that is used to split an application into two or more different modes of operation, most applications have navigation controllers as children of tab bar controllers.

Apple's position is this:

, .

, - -... , , , Nav , . , , , , , Nav Controller. . .

, , , . , .

, :

// 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];

, , .

, .

+12

. , App Delegate...

0

Source: https://habr.com/ru/post/1754052/


All Articles