简体   繁体   English

简单标题未显示在UINavigationController中

[英]Simple Title not showing up in UINavigationController

I have looked at all of the similar/related questions, but none either a) are exactly my problem or 2) the solutions just don't work. 我已经看过所有类似/相关的问题,但是没有一个是a)确实是我的问题,或者2)解决方案根本不起作用。

In my appDelegate.m I have in didFinishLaunchingWithOptions 在我的appDelegate.m中,我在didFinishLaunchingWithOptions中

JCGRootNavigationController *rnc = [[JCGRootNavigationController alloc] init];
self.window.rootViewController = rnc;`

JCGRootNavigationController is a subclass of UINavigationController JCGRootNavigationController是UINavigationController的子类

@interface JCGRootNavigationController : UINavigationController`

In JCGRootNavigationController.m: 在JCGRootNavigationController.m中:

@implementation JCGRootNavigationController

-(instancetype) init {
    self = [super init];
    self.view.backgroundColor = [UIColor lightGrayColor];
    self.navigationItem.title = @"MY TITLE";    
    return self;
}

And the title just won't display. 并且标题将不会显示。 I see the Navigation Bar, but no title. 我看到导航栏,但没有标题。 Looks like lots of people over the years have had this same problem. 这些年来,似乎很多人都遇到了同样的问题。 Maybe a simple answer will help clear up all of the confusing. 也许一个简单的答案将有助于清除所有令人困惑的地方。 This is so incredibly frustrating. 这是如此令人沮丧。

When working with a Storyboard, setting the title of the UIViewController or UITableViewController does not seem to add that title to the Navigation Controller, as other answers suggest. 使用情节提要时,如其他答案所示,设置UIViewControllerUITableViewControllertitle似乎不会将该标题添加到导航控制器。

Instead, find the UINavigationItem that is likely alongside your View Controller object in the Storyboard hierarchy. 而是在Storyboard层次结构中的View Controller对象旁边找到UINavigationItem Set this Navigation Item's title to apply that title to the Navigation Controller. 设置此导航项的title以将该标题应用于导航控制器。

故事板上的导航项[1]

UINavigationController automatically shows the title of the UIViewController subclass it is displaying. UINavigationController自动显示正在显示的UIViewController子类的标题。 It does so by looking at the navigationItem.title property of that UIViewController or UIViewController subclass. 通过查看该UIViewController或UIViewController子类的navigationItem.title属性来实现。 Basically UINavigationController doesn't have a title. 基本上,UINavigationController没有标题。

Thanks believesInSanta, While I cannot find this explicitly stated in apple documentation anywhere, I have to go with this being the answer. 谢谢believesInSanta,尽管我在任何地方的苹果文档中都找不到明确说明的内容,但我必须回答这个问题。 UINavigationController doesn't have a title. UINavigationController没有标题。

To get the title to work, back in appDelegate.h I added: 为了使标题生效,请回到appDelegate.h中,我添加了:

JCGTableViewController *tvc = [[JCGTableViewController alloc] init];
JCGRootNavigationController *rnc = [[JCGRootNavigationController alloc] initWithRootViewController:tvc];
self.window.rootViewController = rnc;

Where JCGTableViewController is another subclass I added. 我添加了JCGTableViewController是另一个子类。 As you can probably tell, it is a subclass of UITableViewController. 如您所知,它是UITableViewController的子类。

In JCGTableViewController I overrode init to: 在JCGTableViewController中,我将init覆盖为:

-(instancetype) init {

    self = [super init];
    if(self) {
        self.title = @"TVC";
        self.view.backgroundColor = [UIColor lightGrayColor];
    }

    return self;
}

While I used a tableViewController, I imagine you can add any view to the NavigationController and set the properties that way. 当我使用tableViewController时,我想您可以将任何视图添加到NavigationController并以这种方式设置属性。 I will play around with that today. 我今天将解决这个问题。

Thanks all! 谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM