xcode 将 UITabBarController 添加到 UIViewController

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12018652/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:15:49  来源:igfitidea点击:

Adding UITabBarController to a UIViewController

iphoneobjective-ciosxcodeuitabbarcontroller

提问by darksky

My app currently has a UINavigationControllerand I'd like to push a UITabBarControllerat some point when a button is clicked. I am trying to create it on Interface Builder (as opposed to programatically).

我的应用程序目前有一个UINavigationController,我想UITabBarController在某个按钮被点击时按下一个。我正在尝试在 Interface Builder 上创建它(而不是以编程方式)。

All online tutorials show how to create a tab bar based app, which involves dragging a UITabBarControllerinto the MainWindow.xibwhich is obviously not what I want.

所有在线教程都展示了如何创建基于标签栏的应用程序,这涉及将 aUITabBarController拖入MainWindow.xib其中,这显然不是我想要的。

What I did was create a UIViewController, and its nib file, dragged a UITabBarController. Now pushing that UIViewControllerto the navigation controller will show an empty view (its empty view). Removing the view in the view controller will crash the app. How can I tell the UIViewControllerto load a UITabBarControllerinstead of its own view?

我所做的是创建UIViewController一个UITabBarController. 现在将其推UIViewController送到导航控制器将显示一个空视图(它的空视图)。删除视图控制器中的视图将使应用程序崩溃。我怎么能告诉UIViewController加载一个UITabBarController而不是它自己的视图?

For those down-voting me: it would be decent to at least provide a comment. The question is not a poor question. The questions is asking for suggestions for how to use a UITabBarController in an unorthodox way. I tried most of the suggestions and they do not work. If you are down-voting, at least write a comment.

对于那些对我投反对票的人:至少提供评论是不错的。这个问题不是一个糟糕的问题。问题是寻求有关如何以非正统方式使用 UITabBarController 的建议。我尝试了大部分建议,但它们不起作用。如果你投反对票,至少写一个评论。

采纳答案by darksky

The problem was, as I believeI've mentioned somewhere, is that the XIB does not have a UIView connected to it. When the UIView is deleted in a XIB file and a UITabBarController is added, the viewproperty of the XIB has to be connected to the UITabBarController's view. I connected it and it worked. That was the reason why I was getting a SIGTRAP.

问题是,正如我相信我在某处提到的那样,XIB 没有连接到它的 UIView。当 UIView 在 XIB 文件中被删除并添加一个 UITabBarController 时,必须view将 XIB的属性连接到UITabBarController的视图。我连接了它并且它起作用了。这就是我收到 SIGTRAP 的原因。

回答by Neo

You can see this this may help you

你可以看到这个这可能对你有帮助

Since this is how you want your app to be: - Navigation Controller - Root View Controller - Other View Controllers - Tab Bar Controller - First VC under tab - Second VC under tab - Third VC under tab - more view controllers

因为这是您希望您的应用程序的样子: - 导航控制器 - 根视图控制器 - 其他视图控制器 - 标签栏控制器 - 标签下的第一个 VC - 标签下的第二个 VC - 标签下的第三个 VC - 更多视图控制器

in your view controller where you want to pushViewController to UITabBarController use this

在你想要 pushViewController 到 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];

回答by jhoanna

This tutorialmight help. It comes with an example code.

教程可能会有所帮助。它带有示例代码

回答by Rajneesh071

Hi just make both nav controller and tabBar Controller in app delegate. Initially add navController to your root view..

嗨,只需在应用程序委托中制作导航控制器和 tabBar 控制器即可。最初将 navController 添加到您的根视图中..

[self.window addSubview:navigationController.view];  

and whenever you want to add tab bar then remove navController and add tabBarController.

并且每当您想添加标签栏时,请删除 navController 并添加 tabBarController。

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

    [[[appdelegte navigationController] view]removeFromSuperview];

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

    [[appdelegte tabcontroller]setSelectedIndex:0];
}  

If you get any problem then ask me again..

如果你有任何问题,然后再问我..

回答by Rajneesh071

In YourView controller make IBOutlet of tabBarController

在 YourView 控制器中制作 tabBarController 的 IBOutlet

in .h file

在 .h 文件中

#import <UIKit/UIKit.h>

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

and in .m file

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

The tabBarController IBOutletmust be connected to the UITabBarControllerthat on the .xib file. And that UITabBarControllerwith two view controllers named FirstViewController, SecondViewController.

tabBarControllerIBOutlet必须连接到UITabBarController.xib 文件上的那个。还有UITabBarController两个名为FirstViewController, 的视图控制器SecondViewController

回答by 08442

take a uiview of tab bar controller means create an interface builder with tabs and add that tab bar uivew in your classes where ever u wanted the tab bar

获取标签栏控制器的 uiview 意味着创建一个带有标签的界面构建器,并在您想要标签栏的类中添加该标签栏 uivew

for example take a tab bar uiview of 3 tabs in that uiview take the three buttons in the interface builder

例如,在该 uiview 中使用 3 个选项卡的选项卡栏 uiview 在界面构建器中使用三个按钮

for every navigation of that classu should add this uiview class

对于该类的每个导航都应添加此 uiview 类

-(IBAction)firt_button_pressed:(id)sender
{

}

-(IBAction)second_button_pressed:(id)sender
{

}

回答by David Evans

I remember doing something similar to this...

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

I had to create a custom UITableViewControllerto do this, if you are going to use UINavigationControllerto 'push' to it.

我必须创建一个自定义UITableViewController来执行此操作,如果您要使用它UINavigationController来“推送”它。

Doing it only in interface builder may be a bit tricky, it's been a while since I've been at it, I do recall it was a bit of a nightmare to get going correctly.

仅在界面生成器中执行此操作可能有点棘手,我已经有一段时间没有使用它了,我确实记得正确进行操作有点像噩梦。