xcode 结合 UITabBarController 和 UINavigationController
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8044668/
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
Combine UITabBarController with UINavigationController
提问by Guido Hendriks
I tried to use an "tabbed application" with a navigation bar in it. With the default the tab bar works fine, but I just can't gat a navigation bar. I found some stuff about pushing the navigation-bar and stuff like that, but all the stuff I found was some years ago, so don't gonna help me. And the recent stuff is outdated to, since iOS5 and the new version of Xcode..
我尝试使用带有导航栏的“选项卡式应用程序”。默认情况下,标签栏工作正常,但我无法打开导航栏。我发现了一些关于推动导航栏之类的东西,但我发现的所有东西都是几年前的,所以不要帮我。最近的东西已经过时了,因为 iOS5 和新版本的 Xcode..
Could anyone point me in the right direction to combine a to solve this problem?
谁能指出我正确的方向来结合 a 来解决这个问题?
Keep the following facts in mind please:
请记住以下事实:
- I'm developing for iOS5
- I'm using Xcode 4.2
- 我正在为 iOS5 开发
- 我正在使用 Xcode 4.2
回答by bryanmac
Here's how you can achieve it programmatically.
以下是您如何以编程方式实现它。
Delete the reference to your main xib in [appName]-Info.plist
删除 [appName]-Info.plist 中对主要 xib 的引用
In main.m, load your delegate:
在 main.m 中,加载您的委托:
int retVal = UIApplicationMain(argc, argv, nil, @"myAppDelegate");
In the app delegate, load the tabBar, the navigation controller and the view in the navigationController.
在应用程序委托中,加载 tabBar、导航控制器和导航控制器中的视图。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// create window since nib is not.
CGRect windowBounds = [[UIScreen mainScreen] applicationFrame];
windowBounds.origin.y = 0.0;
[self setWindow:[[UIWindow alloc] initWithFrame:windowBounds]];
// View Controllers for tabController (one viewController per tab)
NSMutableArray *viewControllers = [[NSMutableArray alloc] init];
// first tab has view controller in navigation controller
FirstView *firstView = [[FirstView alloc] initWithNibName:@"FirstView" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:firstView];
[viewControllers addObject:navController];
SecondView *secondView = [[SecondView alloc] initWithNibName:@"SecondView" bundle:nil];
[viewControllers addObject:secondView];
// create the tab controller and add the view controllers
UITabBarController *tabController = [[UITabBarController alloc] init];
[tabController setViewControllers:viewControllers];
// add tabbar and show
[[self window] addSubview:[tabController view]];
[self.window makeKeyAndVisible];
return YES;
}
回答by Tanuj
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
NSMutableArray *arrayViewController = [[NSMutableArray alloc] init];
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController_iPhone" bundle:nil];
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
[arrayViewController addObject:navigationController1];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController_iPhone" bundle:nil];
UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
[arrayViewController addObject:navigationController2];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = arrayViewController;
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
回答by thpitsch
In iOS 5 it is no longer tolerated to change the view controller for a tab (no problem before iOS5). The only accepted controller is that defined in IB for that tab. So it is neccessary to install a navigation controller on this tab and give his view the navigation bar. Then you can push or pop your desired views without changing the tab's controller.
在 iOS 5 中不再允许更改选项卡的视图控制器(在 iOS5 之前没有问题)。唯一接受的控制器是在 IB 中为该选项卡定义的控制器。所以有必要在这个选项卡上安装一个导航控制器并给他的视图导航栏。然后您可以推送或弹出您想要的视图,而无需更改选项卡的控制器。
回答by MattyG
The basic theory is that you create a UITabBarController, and then put a UINavigationController inside that, and then put a UIViewController as the root view controller of the navigation controller. bryanmac just answered with a good code sample.
基本理论是你创建一个UITabBarController,然后在里面放一个UINavigationController,然后放一个UIViewController作为导航控制器的根视图控制器。bryanmac 刚刚回答了一个很好的代码示例。