以编程方式切换到另一个选项卡不会更改NavigationBar标题

时间:2016-04-08 14:21:48

标签: ios objective-c uinavigationcontroller uitabbarcontroller

我有一个嵌入NavController的TabBarController。我的TabBarController viewDidAppear方法调用此方法根据TabBarItem.tag设置navigationBar:

-(void)updateNaviationWithTabBarItem:(UITabBarItem *)item{
if(item.tag == 1){
    self.title = @"Live Feed";
    NSLog(@"live");
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor], NSFontAttributeName:[UIFont fontWithName:@"Helvetica Neue" size:22]}];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:.4];
    [self.button  setTintColor:[UIColor colorWithRed:0.41 green:0.68 blue:0.9 alpha:1]];
}else if (item.tag == 2){
    self.title = @"Camera";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont fontWithName:@"Helvetica Neue" size:22]}];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.41 green:0.68 blue:0.9 alpha:.4];
    [self.button  setTintColor:[UIColor whiteColor]];
}

用户从CameraViewController上传图片后,该按钮以编程方式更改为:

-(void)presentFeedView {
FeedTableViewController *feedVC = self.tabBarController.viewControllers[0];
[self.tabBarController setSelectedViewController:feedVC];}

当我到达选项卡时,navigationBar会保留之前ViewController的设置。这也发生在其他标签中。

检查tabBarItem.tag后,我是否应该转换到标签? 我应该从其他地方调用此方法吗?

编辑:我可以从日志语句中看出,该方法在最初加载视图时只被调用一次。

1 个答案:

答案 0 :(得分:0)

切换标签时,您需要在视图控制器中手动更改它。通常它在viewWillAppear中,但我不确定当你切换标签时是否会触发,但这是你需要调用的代码。

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    [self setTitle:@"A"];
    self.tabBarController.navigationItem.title = @"A";
}

你可能需要把它放在这里。

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    //change the title based on viewController that is selected
    self.title = @"New title";
}