将UITabBarController添加到UIViewController

3
我的应用当前使用UINavigationController,我想在某个按钮被点击时推送一个UITabBarController。我尝试在Interface Builder中创建它(而不是在代码中)。
所有在线教程都展示如何创建基于标签栏的应用,需要将UITabBarController拖到MainWindow.xib中,但这显然不是我想要的。
我创建了一个UIViewController和它的nib文件,拖了一个UITabBarController。现在将该UIViewController推送到导航控制器将显示一个空视图(它本身的空视图)。从视图控制器中删除视图会导致应用崩溃。我该如何告诉UIViewController加载UITabBarController而不是自己的视图?
对于那些对我进行投票否决的人,请至少提供一条评论。这个问题并不糟糕。这个问题正在寻求如何以非正统的方式使用UITabBarController的建议。我尝试了大多数建议,它们都不起作用。如果你要投票否决,请至少写一条评论。

@aj,我实际上想要我的导航栏在顶部。 - darksky
@Darksky,我已经发布了一个对我有效的解决方案……如果这正是你想要的,请看一下。 - Neo
为什么不使用基于标签栏的应用程序,但将您的子视图控制器_hidesBottomBarOnPush_属性设置为“是”,以便对于所有视图控制器_except_您想显示底部栏的那个视图控制器? - nielsbot
你的意思是不高效?这只是一个简单的操作,不需要编写代码。性能和内存使用都不会成为问题... - nielsbot
@nielsbot,我已解决问题(请见下方答案),而且无需使用任何隐藏的选项卡。我的应用程序太大了,不可能进行如此巨大的更改。 - darksky
显示剩余6条评论
7个回答

14

你可以查看这个链接,它可能会对你有所帮助。

如果你想要你的应用程序具备以下结构: - 导航控制器 - 根视图控制器 - 其他视图控制器 - 标签栏控制器 - 标签下的第一个视图控制器 - 标签下的第二个视图控制器 - 标签下的第三个视图控制器 - 更多视图控制器

在你想要将视图控制器推入UITabBarController的地方使用以下代码:

//create a UITabBarController object
UITabBarController *tabBarController=[[UITabBarController alloc]init];

//FirstViewController and SecondViewController are the view controllers you want on your UITabBarController (Number of view controllers can be according to your need)
FirstViewController *firstViewController=[[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];
SecondViewController *secondViewController=[[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];

//adding view controllers to your tabBarController bundling them in an array
tabBarController.viewControllers=[NSArray arrayWithObjects:firstViewController,secondViewController, nil];

//navigating to the UITabBarController that you created
[self.navigationController pushViewController:tabBarController animated:YES];

1

0

将一个UITabBarController的UIView添加到你的类中,意味着创建一个带有选项卡的Interface Builder,并将该选项卡UIView添加到你想要的选项卡类中。

例如,在一个包含3个选项卡的选项卡UIView中,在Interface Builder中添加三个按钮。

对于该类的每个导航,都应该添加这个UIView类。

-(IBAction)firt_button_pressed:(id)sender
{

}

-(IBAction)second_button_pressed:(id)sender
{

}

0

我记得做过类似的事情...

我不得不创建一个自定义的UITableViewController来完成这个任务,如果你要使用UINavigationController进行‘推送’。

只在界面构建器中完成可能有点棘手,已经有一段时间了,我记得让它正确地开始是有点恶心的。


你是指自定义的 UITabBarController 吗?你具体是怎么做的,有什么建议吗? - darksky

0
问题是,正如我相信我在某个地方提到过的那样,XIB没有与UIView连接。当在XIB文件中删除UIView并添加UITabBarController时,必须将XIB的view属性连接到UITabBarController的视图上。我连接了它,然后它就可以工作了。这就是我收到SIGTRAP的原因。

0

你好,只需在应用程序委托中创建导航控制器和标签栏控制器。最初将导航控制器添加到您的根视图。

[self.window addSubview:navigationController.view];  

每当您想要添加选项卡栏时,请删除 navController 并添加 tabBarController。

-(void)addTabBarController
{
    AppDelegate *appdelegte =(AppDelegate*)[[UIApplication sharedApplication]delegate];

    [[[appdelegte navigationController] view]removeFromSuperview];

    [[appdelegte window]addSubview:[[appdelegte tabcontroller]view]];

    [[appdelegte tabcontroller]setSelectedIndex:0];
}  

如果你遇到任何问题,请再问我一次。

嗨 - 我不想让我的选项卡栏在AppDelegate中。我的应用程序委托转到其他一些视图控制器,其中包含一个导航控制器。从该视图控制器,我有一个按钮,当单击时,我想显示一个选项卡控制器。如何实现?为什么所有选项卡都被认为是整个应用程序的通用选项卡?我所做的是否是不良实践? - darksky
AppDelegate是viewController的起点...在AppDelegate中放置navController和tabBarController...并将导航控制器(前往某个视图)设置为rootController....在按钮点击时,只需删除navController并添加tabBarController... - Rajneesh071
AppDelegate 将为整个应用程序初始化一个 UITabBarController。我的问题明确说明这不是我想要的。我会检查你的其他答案。 - darksky

0
在您的视图控制器中,创建一个tabBarController的IBOutlet。
在.h文件中。
#import <UIKit/UIKit.h>

@interface YourView : UIViewController
{
    IBOutlet UITabBarController *tabBarController;
}
-(IBAction)loadTabBar:(id)sender;
@end  

在 .m 文件中

#import "YourView.h"
#import "FirstViewController.h"
#import "SecondViewController.h"

@implementation YourView

-(IBAction)loadTabBar:(id)sender
{
    FirstViewController *firstView = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
    SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    tabBarController = [[UITabBarController alloc] init];
    tabBarController.viewControllers = [NSArray arrayWithObjects:firstView, secondView, nil];
    [self.navigationController pushViewController:tabBarController animated:YES];
}
@end  

tabBarController的IBOutlet必须连接到.xib文件上的UITabBarController。而这个UITabBarController有两个视图控制器,分别命名为FirstViewControllerSecondViewController


这个不起作用。YourView 需要一个 UIView 才能打开。在 xib 中,您可以选择使用 UITabBarController 或者 UIView。如果有一个 UIView,则选项卡栏将不会显示。如果有一个选项卡栏,则会崩溃,因为在视图控制器中不存在视图。 - darksky
我创建了一个新的nib,删除了视图并添加了一个UITabBarController。我将其连接到属性并使用了上面的代码。我得到了一个SIGTRAP错误。 - darksky
你正在制作哪个类来实现添加 tabBar 的功能?是 UIViewController 还是 UITabBarController? - Rajneesh071
您不需要将tabBarController添加为视图,而是tabBarController管理其他视图控制器。@Rajneesh071的回答是正确的(虽然有点简洁)。 - tapi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接