xcode 如何以编程方式创建标签栏并在其上添加按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3220978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to create a tabbar programmatically and adding buttons on it
提问by Linux world
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...
我有导航控制器,但它不允许我添加这些......我想在我的视图中以编程方式创建......
回答by Jann
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.
希望这可以帮助。
回答by Michael Kessler
You have to keep a reference to the tab bar controller. For instance you might keep it in the App Delegate...
您必须保留对标签栏控制器的引用。例如,您可以将其保存在 App Delegate 中...