在选项卡控制器中设置导航栏的标题

17
我的应用中有一个导航控制器和一个表视图,当按下按钮时会打开一个选项卡控制器。在它的第一个标签中,我想设置导航栏的标题。
这是我的storyboard截图,以确保我们理解清楚。 enter image description here 这是我的代码,应该可以工作。当尝试打开单个视图而不是选项卡控制器时,我使用过这段代码,它也能正常工作。所以我想我必须改变一些东西。
    - (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是我的storyboard中选项卡控制器的标识符。

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中打印出来。


你是否在导航控制器中使用了选项卡控制器? - Lorenzo B
9个回答

39

简单!

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

1
在视图控制器之间切换时,标题文本会消失。 - Suraj K Thomas

18

在ghostrider的解决方案基础上,我的tabbarcontroller.m中的self.navigationItem.title对我不起作用。我有一个导航控制器,其详细视图是一个TabController,这是我第一个选项卡实现的内容。

self.navigationController.visibleViewController.title = ski_center;

这是唯一对我有效的解决方案。我的设置是在UITabbarController(根视图控制器)内部使用UINavigationControllers。不知何故,所有其他明显的解决方案都完全无效。 - Ingweland
这应该是正确的答案。做得好。感谢您的帮助。 - Mike Critchley

8
Swift 4及以上版本中
self.tabBarController?.title = "Title"

6

在Swift 3中

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

6

了解一下,将UITabBarController放在UINavigationController中是非常糟糕的做法 - Apple建议良好的UI设计只应该将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];        
}

那应该可以。

我已经更新了我的答案,请看一下并确认是否可行。 - Wasim
它也没有起作用。你建议我该怎么做?我尝试了这个结构,因为我想从一个表格中打开一个选项卡菜单,而不是打开第一个屏幕就是一个选项卡。 - ghostrider
如果是我,我会忘记标签视图,而是打开另一个带有1-5个不同选项的表格视图,然后将每一行链接到你的新页面。或者,你可以打开另一个视图控制器,并在上面创建一些按钮,再次将每个按钮链接到你的新页面。这两种解决方案都比标签视图控制器更适合你的需求。这样,导航控制器可以轻松展示标题,整个过程对最终用户来说更加合理。 - Wasim
这是一个很好的建议,也很有道理,谢谢你指引我正确的方向。干杯! - ralphgabb
我能否获取苹果在哪里表明这样做不是良好的UI设计的参考链接? - CyberMew

5
我发现了这个问题,并找到了一种更简单的解决方案。一种新的解决方案是从着陆视图控制器更改导航栏标题,如下所示。
override func viewDidLoad() {
    super.viewDidLoad()
    navigationController?.navigationBar.topItem?.title = "title";
 }

2
 UITabBarController *tabController = (UITabBarController *)self.parentViewController;

 tabController.navigationItem.title = @"ABC";

这对我有效。

从互联网上进行了一些研究

你需要将navigationItem传递给上级。 UINavigationController显示属于其topViewController的navigationItem,它是UITabBarController。 UITabBarController在其选项卡中显示navigationItem标题。因此,您需要确保tabBarController的navigationItem是其selectedViewController的navigationItem。

因此,简要概括一下:

UINavigationController标题= topViewController.navigationItem.title

UITabBarController选项卡标题= selectedViewController.navigationItem.title

UIViewController标题= navigationItem.title


1

试试这个:

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

1
基本上我不知道你是否注意到了,但是我已经让它像这样工作了:
我给我的选项卡控制器分配了一个新类。
我做了以下事情:
将其添加到应用程序委托.h中。
@property (nonatomic,retain) NSString *center;

将其添加到mainmap.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;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接