1

我创建了一个选项卡式应用程序,每个选项卡有 4 个选项卡和 4 个视图控制器。我尝试在 FirstViewController 中添加导航栏,它只添加导航栏,内容区域显示为黑色。

  • 如何组合导航控制器、选项卡和视图控制器?
  • 组合 ViewController 的可能方式有哪些?

我的选项卡和视图控制器代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    UIViewController *viewController1, *viewController2,*viewController3, *viewController4;
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        viewController1 = [[VKFirstViewController alloc] initWithNibName:@"VKFirstViewController_iPhone" bundle:nil];
        viewController2 = [[VKSecondViewController alloc] initWithNibName:@"VKSecondViewController_iPhone" bundle:nil];
        viewController3 = [[VKThirdViewController alloc] initWithNibName:@"VKThirdViewController_iPhone" bundle:nil];
        viewController4 = [[VKFourthViewController alloc] initWithNibName:@"VKFourthViewController_iPhone" bundle:nil];

    } else {
        viewController1 = [[VKFirstViewController alloc] initWithNibName:@"VKFirstViewController_iPad" bundle:nil];
        viewController2 = [[VKSecondViewController alloc] initWithNibName:@"VKSecondViewController_iPad" bundle:nil];
        viewController3 = [[VKThirdViewController alloc] initWithNibName:@"VKThirdViewController_iPad" bundle:nil];
        viewController4 = [[VKFourthViewController alloc] initWithNibName:@"VKFourthViewController_iPad" bundle:nil];

    }
    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = @[viewController1, viewController2,viewController3, viewController4];
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}
4

2 回答 2

1
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.

    UINavigationController *firstNav = [[UINavigationController alloc]init];
    UINavigationController *secondNav = [[UINavigationController alloc]init];


    UIViewController *viewController1, *viewController2,*viewController3, *viewController4;
      if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    viewController1 = [[VKFirstViewController alloc] initWithNibName:@"VKFirstViewController_iPhone" bundle:nil];
    viewController2 = [[VKSecondViewController alloc] initWithNibName:@"VKSecondViewController_iPhone" bundle:nil];
    viewController3 = [[VKThirdViewController alloc] initWithNibName:@"VKThirdViewController_iPhone" bundle:nil];
    viewController4 = [[VKFourthViewController alloc] initWithNibName:@"VKFourthViewController_iPhone" bundle:nil];
  firstNav.viewControllers =  @[viewController1, viewController2,viewController3, viewController4];

} else {
    viewController1 = [[VKFirstViewController alloc] initWithNibName:@"VKFirstViewController_iPad" bundle:nil];
    viewController2 = [[VKSecondViewController alloc] initWithNibName:@"VKSecondViewController_iPad" bundle:nil];
    viewController3 = [[VKThirdViewController alloc] initWithNibName:@"VKThirdViewController_iPad" bundle:nil];
    viewController4 = [[VKFourthViewController alloc] initWithNibName:@"VKFourthViewController_iPad" bundle:nil];
    secondNav.viewControllers =  @[viewController1, viewController2,viewController3, viewController4];
}
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = @[firstnav, secondNav];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;

}

于 2013-05-09T07:12:40.393 回答
0

我在 navigationController 中添加了 viewController1,并将这个 navigationController 对象添加到 tabBarController。现在它工作正常。参考https://stackoverflow.com/a/14030551/1119440

UIViewController *tmpviewController1, *viewController2,*viewController3, *viewController4;
        UINavigationController *viewController1;


        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {

            //UIViewController *tmpViewController1 = [[[YourViewController alloc] init] initWithNibName:@"YourViewController" bundle:nil];
            tmpviewController1 = [[VKFirstViewController alloc] initWithNibName:@"VKFirstViewController_iPhone" bundle:nil];
            viewController1 =  [[UINavigationController alloc] initWithRootViewController:tmpviewController1];

            //viewController1 = [[VKFirstViewController alloc] initWithNibName:@"VKFirstViewController_iPhone" bundle:nil];
            viewController2 = [[VKSecondViewController alloc] initWithNibName:@"VKSecondViewController_iPhone" bundle:nil];
            viewController3 = [[VKThirdViewController alloc] initWithNibName:@"VKThirdViewController_iPhone" bundle:nil];
            viewController4 = [[VKFourthViewController alloc] initWithNibName:@"VKFourthViewController_iPhone" bundle:nil];

        } else {
            viewController1 = [[VKFirstViewController alloc] initWithNibName:@"VKFirstViewController_iPad" bundle:nil];
            viewController2 = [[VKSecondViewController alloc] initWithNibName:@"VKSecondViewController_iPad" bundle:nil];
            viewController3 = [[VKThirdViewController alloc] initWithNibName:@"VKThirdViewController_iPad" bundle:nil];
            viewController4 = [[VKFourthViewController alloc] initWithNibName:@"VKFourthViewController_iPad" bundle:nil];

        }
        self.tabBarController = [[UITabBarController alloc] init];
        self.tabBarController.viewControllers = @[viewController1, viewController2,viewController3, viewController4];
于 2013-05-09T07:10:48.630 回答