objective-c 如何将 TabBar + Navigation 与 XCode 结合使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/478448/
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 combine TabBar + Navigation with XCode
提问by mamcx
I'm triying to combine a TabBar + Navigation app.
我正在尝试结合 TabBar + Navigation 应用程序。
I have 5 tab bars, 4 are listing of stuff and drill down to details views.
我有 5 个标签栏,其中 4 个是内容列表并深入到详细信息视图。
I try to follow this tutorial:
我尝试按照本教程进行操作:
http://www.iphonedevforums.com/forum/iphone-sdk-development/124-view-controller-problem.html
http://www.iphonedevforums.com/forum/iphone-sdk-development/124-view-controller-problem.html
But always get a blank view.
但总是得到一个空白的视图。
This is what I do, with a clean project:
这就是我所做的,有一个干净的项目:
- I start with a TabBar template app.
- I put 5 tab bar buttons.
I create a controller like:
@interface FirstViewController : UINavigationController {
}
- I put the main window.xib on tree mode & change the selected first view to
FirstViewController - I select the TabBar Controller in Interface builder, go to TabBar Attributes & change the class to navigation controler.
- Select the fist view & put the nib name "SecondView"
- 我从 TabBar 模板应用程序开始。
- 我放了 5 个标签栏按钮。
我创建了一个控制器,如:
@interface FirstViewController : UINavigationController {
}
- 我将主 window.xib 置于树模式并将选定的第一个视图更改为
FirstViewController - 我在界面构建器中选择 TabBar 控制器,转到 TabBar 属性并将类更改为导航控制器。
- 选择第一个视图并输入笔尖名称“SecondView”
In response, I get a blank screen.
作为回应,我得到一个空白屏幕。
I must add that I wanna navigate from the details views, no from the main windows.
我必须补充一点,我想从详细信息视图中导航,而不是从主窗口中导航。
i.e in the main window tab bar 1 is the list of people. I select a person then wanna navigate to the detail window.
即在主窗口标签栏 1 是人员列表。我选择一个人然后想要导航到详细信息窗口。
回答by Andrey Tarantsov
First, you never want to subclass UINavigationController or UITabBarController.
首先,你永远不想继承 UINavigationController 或 UITabBarController。
Second, I did not quite get what you did, but a correct sequence to create a navigation controller inside a tab bar controller is something like this:
其次,我不太明白你所做的,但是在标签栏控制器中创建导航控制器的正确顺序是这样的:
// in MyAppDelegate.h
UINavigationController *nc1, *nc2;
FirstTabRootViewController *vc1;
SecondTabRootViewController *vc2;
UITabBarController *tbc;
// in MyAppDelegate.m
nc1 = [[UINavigationController alloc] init];
vc1 = [[FirstTabRootViewController alloc] initWithNibName:nil bundle:nil];
vc1.tabBarItem.title = @"Tab 1";
vc1.tabBarItem.image = [UIImage imageNamed:@"tab1.png"];
vc1.navigationItem.title = "Tab 1 Data";
nc1.viewControllers = [NSArray arrayWithObjects:vc1, nil];
nc2 = [[UINavigationController alloc] init];
vc2 = [[SecondTabRootViewController alloc] initWithNibName:nil bundle:nil];
vc2.tabBarItem.title = @"Tab 2";
vc2.tabBarItem.image = [UIImage imageNamed:@"tab2.png"];
vc2.navigationItem.title = "Tab 2 Data";
nc2.viewControllers = [NSArray arrayWithObjects:vc2, nil];
tbc = [[UITabBarController alloc] init];
tbc.viewControllers = [NSArray arrayWithObjects:nc1, nc2, nil];
Note that it's your view controller that controls the text/icon in the tab bar and in the navigation bar. Create a UINavigationController instance for each of your tabs; UINavigationController contains a stack of view controllers (see viewControllers property) which should contain at least one item — your root controller for that tab. Also create an UITabBarController to manage the tabs.
请注意,是您的视图控制器控制选项卡栏和导航栏中的文本/图标。为每个选项卡创建一个 UINavigationController 实例;UINavigationController 包含一堆视图控制器(请参阅 viewControllers 属性),其中至少应包含一个项目 - 该选项卡的根控制器。还要创建一个 UITabBarController 来管理选项卡。
Of course, you can (and probably should) use interface builder instead of code to instantiate the mentioned classes and set the properties. But it's important that you understand what happens behind the scenes; interface builder is nothing more than a convenient way to instantiate and set up objects.
当然,您可以(并且可能应该)使用界面构建器而不是代码来实例化上述类并设置属性。但了解幕后发生的事情很重要;interface builder 只不过是一种方便的方式来实例化和设置对象。
Hope this is helpful; please refine your question if it's not.
希望这有帮助;如果不是,请完善您的问题。
回答by success_anil
Still getting the blank screen On Starting the application after implementing the above code. Where i 'm writing it wrong.
执行上述代码后,启动应用程序时仍然出现空白屏幕。我哪里写错了。
nc1 = [[UINavigationController alloc] init];
nc2 = [[UINavigationController alloc] init];
vc1 = [[FirstRootViewController alloc]initWithNibName:@"FirstRootViewController" bundle:nil];
vc1.tabBarItem.title = @"Item 1";
vc1.tabBarItem.image= [UIImage imageNamed:@"home.png"];
vc1.navigationItem.title = @"Tab1 Data";
nc1.viewControllers = [NSArray arrayWithObjects:vc1,nil];
vc2 = [[SecondRootViewController alloc]initWithNibName:@"SecondRootViewController" bundle:nil];
vc2.tabBarItem.title = @"Item 2";
vc2.tabBarItem.image= [UIImage imageNamed:@"home.png"];
vc2.navigationItem.title = @"Tab2 Data";
nc2.viewControllers = [NSArray arrayWithObjects:vc2,nil];
tbc = [[UITabBarController alloc]init];
tbc.viewControllers = [NSArray arrayWithObjects:nc1,nc2,nil];
[window addSubview:tbc.view];
[window makeKeyAndVisible];
回答by bentford
Here is an tutorialthat I was able to get working.
这是我能够开始工作的教程。
I also read the official SDK documentation on the topic: Combining Tab Bar and Navigation Controllers. Since I'm still learning, the tutorial helped me more than the docs.
我还阅读了有关该主题的官方 SDK 文档: Combining Tab Bar and Navigation Controllers。由于我仍在学习,因此教程比文档对我的帮助更大。
NOTE: in the tutorial, i don't think you need to subclass UINavigationController, and I'm experimenting with this idea now.
注意:在本教程中,我认为您不需要继承 UINavigationController,我现在正在试验这个想法。
回答by user118814
I tried to create an iphone application with UITabBarController and some UINavigationController inside it and faced the same problems as "mamcx". With your example-code i get it to run :) Thanks a lot.
我尝试使用 UITabBarController 和其中的一些 UINavigationController 创建一个 iphone 应用程序,并面临与“mamcx”相同的问题。使用您的示例代码,我可以运行它:) 非常感谢。
Here is how it works for me.
这是它对我的工作方式。
// YOURS
fourthNavigation = [[UINavigationController alloc ] init ];
fourthViewController = [[[FourthTabRootController alloc] initWithNibName:@"FourthView" bundle:nil] autorelease];
fourthNavigation.tabBarItem.title = @"YOURS";
fourthNavigation.viewControllers = [NSArray arrayWithObjects:fourthViewController, nil];
// Add self-defined UIViewControllers to the tab bar
tabBarController.viewControllers = [NSArray arrayWithObjects:firstNavigation,secondNavigation, thirdNavigation, fourthNavigation, nil];
// Add the tab bar controller's current view as a subview of the window
[window addSubview:tabBarController.view];
The other UINavigationControllers "firstNavigation, ... " are created the same way. I load the view elements from nib-files that are connected to my subclassed UIViewController classes. You dont need to add a NavigationBar in the IB to your view, cause the UINavigationController has allready one. So you only need to set the title in "initWithNibName"
其他 UINavigationControllers "firstNavigation, ... " 以相同的方式创建。我从连接到我的子类 UIViewController 类的 nib 文件加载视图元素。您不需要在 IB 中将 NavigationBar 添加到您的视图中,因为 UINavigationController 已经有了一个。所以你只需要在“initWithNibName”中设置标题
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
self.title = @"YOURS";
}
return self;
}
I hope that helps.
我希望这有帮助。
回答by bhavinb
Check out "Adding a Navigation Controller to a Tab Bar Interface" under View Controller Catalog for iOSwhich take you step by step into how exactly this can be achieved
查看iOS 视图控制器目录下的“将导航控制器添加到选项卡栏界面”,它将带您逐步了解如何实现这一点

