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

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

问题描述

我想在我的视图中以编程方式将按钮添加到我的标签栏...

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.

苹果的立场是这样的:

您使用标签栏控制器您的应用程序的情况要么呈现不同类型的数据或呈现相同的数据明显不同的方式.

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.

希望这会有所帮助.

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

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