[英]TableViewController is causing TabBar item title to change to navigation bar title
[英]Can not set title in Navigation bar when click on tabbar item in iOS
我嘗試在UIViewcontroller中創建UItabbarcontroller。 但是,當我單擊選項卡項(firstview)時,它在根viewcontroller的頂部顯示了導航欄。 我設置了標題,但未顯示。
代碼創建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
}
請看這張圖片: 它只顯示按鈕Back,不顯示標題。 我不知道為什么
像這樣設置:
-(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
}
看起來您正在將UITabBarController
添加到UINavigationController
,那么您需要執行以下操作:
self.parentViewController.navigationItem.title = [NSString stringWithFormat: @"Hello, %@",userLogin1];
如果要在導航欄中設置圖像或顏色,請使用此代碼。
// 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";
在UItabbarcontroller
為每個ViewController
設置UINavigationController
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:controller_1];
並像這樣為每個ViewController
設置標題
self.title = @"yourTitle";
你可以這樣設置
[self.tabBarController setTitle:@"Title"];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.