单击iOS中的tabbar项时,无法在导航栏中设置标题

时间:2013-08-09 10:50:38

标签: ios objective-c uiviewcontroller uitabbarcontroller

我尝试在UIViewcontroller中创建UItabbarcontroller。但是当我单击tabbar项目(firstview)时,它会在根视图控制器的顶部显示导航栏。我设置了标题,但没有显示。

代码创建UITabBarcontroller:

self.tab=[[UITabBarController alloc]init];

// FirstViewController
First *fvc=[[First alloc]initWithNibName:nil bundle:nil];
fvc.title=@"First";
fvc.tabBarItem.image=[UIImage imageNamed:@"i.png"];

//SecondViewController
Second *svc=[[Second alloc]initWithNibName:nil bundle:nil];
svc.title=@"Second";
svc.tabBarItem.image=[UIImage imageNamed:@"im.png"];

//ThirdViewController
Third *tvc=[[Third alloc]initWithNibName:nil bundle:nil];
tvc.title=@"Third";
tvc.tabBarItem.image=[UIImage imageNamed:@"img.png"];

self.tab.viewControllers=[NSArray arrayWithObjects:fvc, svc, tvc, nil];

[self.view addSubview:self.tab.view];

Firstviewcontroller中的代码:

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    userLogin1 =[[NSUserDefaults standardUserDefaults]valueForKey :@"textfieldtext"];
    NSLog(@"User login: %@",userLogin1);

    self.navigationItem.title = [NSString stringWithFormat: @"Hello, %@",userLogin1]; // it not works

}

请看这个图片: enter image description here 它只显示按钮返回,它不显示标题。我不知道为什么。

5 个答案:

答案 0 :(得分:3)

设置如下:

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    userLogin1 =[[NSUserDefaults standardUserDefaults]valueForKey :@"textfieldtext"];
    NSLog(@"User login: %@",userLogin1);
//Here
    self.title = [NSString stringWithFormat: @"Hello, %@",userLogin1]; // it not works

}

答案 1 :(得分:2)

您似乎要将UITabBarController添加到UINavigationController,然后您需要执行以下操作:

self.parentViewController.navigationItem.title = [NSString stringWithFormat: @"Hello, %@",userLogin1];

答案 2 :(得分:0)

如果要在导航栏中设置图像或颜色,请使用此代码..

     // For Background Image
     [[UINavigationBar appearance] setBackgroundImage:[UIImage 
     imageNamed:@"TitleImage.png"]] forBarMetrics:UIBarMetricsDefault];

     // For Background Color
     [[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor 
     whiteColor]] forBarMetrics:UIBarMetricsDefault];

对于导航栏标题,请使用此代码...

     self.title = NSLocalizedString(@"FirstViewControllerTitle",nil);

                                     OR

     self.title = @"FirstViewControllerTitle";

答案 3 :(得分:0)

UINavigationController

中的每个ViewController设置UItabbarcontroller
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:controller_1];

并为每个ViewController设置标题

self.title = @"yourTitle";

答案 4 :(得分:0)

您可以像这样设置

[self.tabBarController setTitle:@"Title"];