将UITabBarController与UINavigationController结合使用

3
我尝试使用一个带有导航栏的“选项卡应用程序”。 默认情况下,选项卡功能正常,但我无法得到导航栏。我找到了一些关于推动导航栏等内容的信息,但所有我找到的东西都是几年前的,所以不会对我有所帮助。最近的信息也已过时,因为iOS5和Xcode的新版本...
请问有谁能指点我正确的方向来解决这个问题?
请记住以下事实:
  • 我正在开发iOS5版本
  • 我正在使用Xcode 4.2
4个回答

14

以下是如何通过编程实现此操作。

请在 [appName]-Info.plist 中删除与您主要 xib 相关的参考。

在 main.m 文件中加载您的代理:

int retVal = UIApplicationMain(argc, argv, nil, @"myAppDelegate");

在应用程序代理中,加载tabBar、导航控制器和视图到navigationController中。

- (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;
}

第一个视图会有一个导航栏,而第二个视图则是一个简单的视图,对吧? - Guido Hendriks
正确 - 只有第一个视图被包装在导航控制器中。选项卡栏中的其他视图可以是您想要的任何内容。 - bryanmac
你是我的英雄!它能够正常工作,现在终于有意义了。:) 谢谢你! - Guido Hendriks

6
- (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;
}

2
在iOS 5中,不再允许更改选项卡的视图控制器(在iOS5之前没有问题)。唯一接受的控制器是在IB中为该选项卡定义的控制器。因此,在该选项卡上安装导航控制器并给其视图添加导航栏是必要的。然后,您可以推入或弹出所需的视图,而无需更改选项卡的控制器。

1
基本理论是创建一个UITabBarController,然后在其中放置一个UINavigationController,再将一个UIViewController作为导航控制器的根视图控制器。bryanmac刚刚提供了一个很好的代码示例。

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