如何以编程方式创建的TabBar并在其上添加按钮 [英] how to create a tabbar programmatically and adding buttons on it

查看:91
本文介绍了如何以编程方式创建的TabBar并在其上添加按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的标签栏在我看来,编程按钮添加...

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

推荐答案

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

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.

希望这有助于。

这篇关于如何以编程方式创建的TabBar并在其上添加按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆