如何以编程方式创建选项卡栏并在其上添加按钮

发布于 2024-09-09 02:57:31 字数 87 浏览 2 评论 0原文

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

我有导航控制器,但它不允许我添加这些按钮.. 在我看来,我想以编程方式创建......

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笔芯 2024-09-16 02:57:31

由于选项卡栏控制器是一个容器视图控制器,可用于将应用程序划分为两个或多个不同的操作模式,因此大多数应用程序都将导航控制器作为选项卡栏控制器的子级。

苹果的立场是这样的:

您在以下位置使用标签栏控制器
您的申请的情况
要么呈现不同类型
数据或呈现相同的数据
显着不同的方式。

这并不是说您不能以不同的方式做事...您遇到的主要问题是您已经在应用程序中放置了一个导航控制器,并且您希望以编程方式创建选项卡栏控制器。因此,我能看到这一点的唯一方法是,您不介意每次在导航控制器中更改屏幕时标签栏控制器是否发生变化。有些应用程序以这种方式工作。大多数没有。

如果我的上述假设是正确的,我建议您重新考虑您的代码,看看您是否想继续这条开发路线。如果是这样,您可以轻松创建选项卡栏控制器并将其附加到当前视图中。

以下是我用来为我的一个应用程序创建设置的代码:

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

在很多情况下,我觉得以编程方式完成所有操作会更容易。

希望这有帮助。

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.

不念旧人 2024-09-16 02:57:31

您必须保留对选项卡栏控制器的引用。例如,您可以将其保留在应用程序委托中......

You have to keep a reference to the tab bar controller. For instance you might keep it in the App Delegate...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文