将UITabBarController与UINavigationController结合使用

Gui*_*iks 3 iphone xcode cocoa-touch objective-c ios

我尝试使用带有导航栏的"选项卡式应用程序".使用默认标签栏工作正常,但我只是无法gat导航栏.我找到了一些关于推动导航栏和类似东西的东西,但我发现的所有东西都是几年前的,所以不要帮助我.最近的东西已经过时,因为iOS5和Xcode的新版本..

任何人都能指出我正确的方向来结合一个来解决这个问题吗?

请记住以下事实:

  • 我正在为iOS5开发
  • 我正在使用Xcode 4.2

bry*_*mac 14

以下是如何以编程方式实现它.

在[appName] -Info.plist中删除对主xib的引用

在main.m中,加载您的委托:

int retVal = UIApplicationMain(argc, argv, nil, @"myAppDelegate");
Run Code Online (Sandbox Code Playgroud)

在app delegate中,加载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;
}
Run Code Online (Sandbox Code Playgroud)


小智 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;
}
Run Code Online (Sandbox Code Playgroud)