且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何以编程方式创建的TabBar并在其上添加按钮

更新时间:2023-02-12 19:19:17

由于标签栏控制器是你使用你的应用程序划分为操作两个或多个不同模式的容器视图控制器,大多数应用程序具有导航控制器的孩子的标签栏控制器。

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.

苹果的地位是这样的:

您使用标签栏控制器中
  情况下您的应用程序
  任一presents不同类型的
  数据或者presents在同一数据
  显著不同的方式。

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.

这并不是说你不能做一些与众不同的事情......你拥有的主要问题是,你已经放置了导航控制器的应用程序,你想通过程序生成标签栏控制器。因此,我可以看到的唯一方法是,如果使用TabBar控制器每次更改导航控制器内的屏幕时间的变化,你不要介意。一些应用程序以这种方式工作。大多数没有。

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.

如果以上我的假设是真的,我会建议你重新考虑你的code,看看你想追求这条线的发展。如果是这样,你可以轻松地创建一个的TabBar控制器和当前视图中附加。

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.

下面是code我用它来创建我的设置为我的应用程序之一:

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.

希望这有助于。