导航栏没有显示标题

时间:2014-12-20 17:28:13

标签: ios uinavigationbar

我无法让导航栏显示我指定的标题。我试图在AppDelegate.m以及我的第一个标签视图的viewDidLoad中更改标题。我怀疑标题是隐藏的,但我无法修复它。请提供您可能有的任何见解。

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

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

    navigationController = [[UINavigationController alloc] initWithNavigationBarClass:[CRGradientNavigationBar class] toolbarClass:nil];


    UIColor *firstColor = [UIColor colorWithRed:155.0f/255.0f green:41.0f/255.0f blue:104.0f/255.0f alpha:1.0f];
    UIColor *secondColor = [UIColor colorWithRed:215.0f/255.0f green:90.0f/255.0f blue:18.0f/255.0f alpha:1.0f];

    NSArray *colors = [NSArray arrayWithObjects:firstColor, secondColor, nil];

    [[CRGradientNavigationBar appearance] setBarTintGradientColors:colors];

    self.navigationController.title = @"This title is not showing";

    [[navigationController navigationBar] setTranslucent:NO]; 


    //create the view controller for the first tab
    self.firstViewController = [[FirstViewController alloc] initWithNibName:nil
                                                                     bundle:NULL];

    //create the view controller for the second tab
    self.secondViewController = [[SecondViewController alloc] initWithNibName:nil
                                                                       bundle:NULL];

    //create the view controller for the third tab
    self.thirdViewController = [[ThirdViewController alloc] initWithNibName:nil
                                                                       bundle:NULL];

    //create the view controller for the fourth tab
    self.fourthViewController = [[FourthViewController alloc] initWithNibName:nil
                                                                     bundle:NULL];

    //create an array of all view controllers that will represent the tab at the bottom
    NSArray *myViewControllers = [[NSArray alloc] initWithObjects:
                                  self.firstViewController,
                                  self.secondViewController, self.thirdViewController, self.fourthViewController, nil];

    //initialize the tab bar controller
    self.tabBarController = [[MainUITabBarController alloc] init];

    //set the view controllers for the tab bar controller
    [self.tabBarController setViewControllers:myViewControllers];


    [navigationController setViewControllers:@[self.tabBarController]];

    [self.window setRootViewController:navigationController];

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];  

    return YES;

}

2 个答案:

答案 0 :(得分:3)

视图控制器的关系不正确。

  1. 您应用的根视图控制器应该是TabBarController。

  2. 每个项目都应该有自己的导航控制器。

  3. 可以使用viewDidLoad()self.title = "...中设置每个ViewController的标题。

答案 1 :(得分:2)

我已经能够在委托中设置标题,如下所示: 1.)创建视图控制器 2.)设置这些标题 3.)然后创建UINavigationControllers并在那里分配视图控制器

self.firstViewController = [[FirstViewController alloc] initWithNibName:nil bundle:nil];
self.secondViewController = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
self.thirdViewController = [[ThirdViewController alloc] initWithNibName:nil bundle:nil];
self.fourthViewController = [[FourthViewController alloc] initWithNibName:nil bundle:nil];

[self.firstViewController setTitle:@"First"];
[self.secondViewController setTitle:@"Second"];
[self.thirdViewController setTitle:@"Third"];
[self.fourthViewController setTitle:@"Fourth"];

UINavigationController *controller1 = [[UINavigationController alloc] initWithRootViewController:self.firstViewController];
UINavigationController *controller2 = [[UINavigationController alloc] initWithRootViewController:self.secondViewController];
UINavigationController *controller3 = [[UINavigationController alloc] initWithRootViewController:self.thirdViewController];
UINavigationController *controller4 = [[UINavigationController alloc] initWithRootViewController:self.fourthViewController];

NSArray *viewControllers = [NSArray arrayWithObjects:controller1, controller2, controller3, controller4, nil];