【问题标题】:Programmatically switching to another tab does not change NavigationBar Title以编程方式切换到另一个选项卡不会更改 NavigationBar 标题
【发布时间】:2016-04-08 14:21:47
【问题描述】:

我有一个嵌入在 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 后转换到选项卡吗? 我应该从其他地方调用这个方法吗?

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

【问题讨论】:

  • 这可能对你有帮助 self.navigationController.navigationBar.topItem.title = @"YOUR_Title_text";
  • 为什么不调试代码并检查控制是否进入 if block 或 else block...

标签: ios objective-c uinavigationcontroller uitabbarcontroller


【解决方案1】:

切换选项卡时,您需要在视图控制器中手动更改它。通常它在 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";
}

【讨论】:

  • 我应该提到的。我有三个 VC 是 FeedViewController 的子类,所以我需要检查我认为的标签索引或标签
  • 对,保存数据并通过控制器ID或其他东西对其进行索引。您可以让每个人都实现该方法,以便在调用它时正确显示正确的标题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-07
  • 2011-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-07
相关资源
最近更新 更多