【问题标题】:Setting the title of a navigation bar inside a tab bar controller在标签栏控制器内设置导航栏的标题
【发布时间】:2026-01-20 04:35:02
【问题描述】:

在我的应用程序中,我有一个带有表格视图的导航控制器,当按下按钮时,一个选项卡控制器会打开。在它的第一个选项卡中,我希望设置导航栏的标题。

这是我的故事板的图像,只是为了确保我们彼此不了解。

这是我应该可以工作的代码。我在尝试打开单个视图而不是选项卡控制器时使用了此代码,并且它工作正常。所以我想我必须改变一些东西。

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{       
        SkiCenter *myDetViewCont = [[SkiCenter alloc] initWithNibName:@"SkiCenter" bundle:[NSBundle mainBundle]];
        myDetViewCont.ski_center = [centers objectAtIndex:indexPath.row];
        UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
        [[self navigationController] pushViewController:[storyBoard instantiateViewControllerWithIdentifier:@"SkiCenterIds"] animated:YES];
        //[self.navigationController pushViewController:myDetViewCont animated:YES]; // "Pushing the controller on the screen"
        [myDetViewCont release]; // releasing controller from the memory
        myDetViewCont = nil;        
    }

Skicenter 是我的第一个选项卡的类名称,SkiCenterIds 是我在情节提要中的选项卡控制器的标识符。

Skicenter.m 中的代码是:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    self.navigationItem.title = ski_center;        
}

但我没有看到任何标题。那么我做错了什么? 我也试过这个:

[self.navigationController setTitle:@"Live"];

self.title=ski_center;

ski_center 之所以有价值,是因为它在 NSLog 中正常打印出来。

【问题讨论】:

  • 您是否在导航控制器中使用标签栏控制器?

标签: ios uinavigationcontroller uitabbarcontroller titlebar


【解决方案1】:

我遇到了这个并找到了一个更简单的解决方案。一个新的解决方案是从着陆视图控制器更改导航栏标题,如下所示

override func viewDidLoad() {
    super.viewDidLoad()
    navigationController?.navigationBar.topItem?.title = "title";
 }

【讨论】:

    【解决方案2】:

    Swift 4 及更高版本中

    self.tabBarController?.title = "Title"
    

    【讨论】:

      【解决方案3】:

      在 Swift 3 中

      self.tabBarController?.navigationItem.title = "Your title goes here"
      

      【讨论】:

        【解决方案4】:
         UITabBarController *tabController = (UITabBarController *)self.parentViewController;
        
         tabController.navigationItem.title = @"ABC";
        

        这对我有用

        来自互联网上的一些研发

        您必须将 navigationItem 向上传递。 UINavigationController 显示属于其 topViewController 的导航项,即 UITabBarController。 UITabBarController 在其选项卡中显示 navigationItem 标题。所以你需要做的是确保tabBarController的navigationItem是selectedViewController的navigationItem

        回顾一下:

        UINavigationController 标题 = topViewController.navigationItem.title

        UITabBarController tabTitle = selectedViewController.navigationItem.title

        UIViewController 标题 = navigationItem.title

        【讨论】:

          【解决方案5】:

          简单!

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

          【讨论】:

          • 在视图控制器之间切换时标题文本消失
          【解决方案6】:

          添加到 ghostrider 的解决方案,在 mytabbarcontroller.m self.navigationItem.title 对我不起作用。我有一个 NavigationController,它的详细视图是一个 TabController,这就是我在第一个选项卡实现中所拥有的

          self.navigationController.visibleViewController.title = ski_center;
          

          【讨论】:

          • 这是唯一对我有用的解决方案。我的设置 - UITabbarController 内的 UINavigationControllers (这是根视图控制器)。不知何故,所有其他明显的解决方案根本不起作用。
          • 这应该是正确的答案。做得很好。感谢您的帮助。
          【解决方案7】:

          基本上我不知道你是否没有注意到,但我是这样工作的:

          我为我的标签栏控制器分配了一个新类。

          我做了以下事情:

          将其添加到 app delegate.h

          @property (nonatomic,retain) NSString *center;
          

          将其添加到 ma​​inmap.m

          AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
              delegate.center=[centers objectAtIndex:indexPath.row];
          

          mytabbarcontroller.m

            AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
              ski_center=delegate.center;
          
              self.navigationItem.title = ski_center;
          

          【讨论】:

            【解决方案8】:

            好吧,你知道,将UITabBarController 放在UINavigationController 中是非常糟糕的做法——Apple 建议您只应将UINavigationController 放在UITabBarController 中而不是另一个一路走来。无论哪种方式,您都应该可以像这样从UITabBarController 更改标题;

            - (void)viewDidLoad
            {
                [super viewDidLoad];
                // Do any additional setup after loading the view from its nib.
            
                [[self.navigationController.viewControllers objectAtIndex:0] setTitle:ski_center];        
            }
            

            应该可以的。

            【讨论】:

            • 我已经更新了我的答案,请看看是否有效。
            • 它也没有用。你建议我怎么做?我尝试了这种结构,因为我想从表中打开一个选项卡菜单,而不是第一个打开为选项卡的屏幕。
            • 如果是我,我会忘记标签视图,只打开另一个表视图,其中包含您通常在标签上拥有的 1-5 个不同选项,然后将每一行链接到新页面。或者,您可以打开另一个视图控制器并在其上创建一些按钮,再次将每个按钮链接到您的新页面。对于您所需要的,这两种解决方案都比选项卡视图控制器更好。这样,导航控制器可以轻松显示标题,并且该过程对最终用户来说更有意义。
            • 这是一个很好的建议,也很有意义,感谢您为我指明了正确的方向。干杯
            • 我可以获取参考链接,Apple 说这样做不是好的 UI 设计吗?
            【解决方案9】:

            试试这个:

            [self.navigationController setTitle:@"Live"];
            

            【讨论】:

              最近更新 更多