简体   繁体   English

标题和栏按钮未显示在导航栏中

[英]Title and bar button not displaying in navigation bar

I'm having some strange difficulties with setting a title and adding a bar button to my navigation bar. 我在设置标题和向导航栏中添加栏按钮时遇到一些奇怪的困难。

My app is a Tabbed application. 我的应用程序是选项卡式应用程序。 The problem is within my first tab. 问题在我的第一个选项卡中。 The first tab consists of a UINavigationController, that contains a UITableViewController. 第一个选项卡包含一个UINavigationController,其中包含一个UITableViewController。 From my AppDelegate: 从我的AppDelegate:

UIViewController *firstViewController = [[MyTableViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];

_tabBarController = [[TFTabBarController alloc] init];
_tabBarController.delegate = self;
_tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController, nil];

On selecting a row in firstViewController, I push a UIWebViewController on the navigation stack: 在firstViewController中选择一行时,我将UIWebViewController推入导航堆栈:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIViewController *secondViewController = [[MyWebViewController alloc] init];
    [self.navigationController pushViewController:secondViewController animated:YES];
}

On intercepting the onclick of a hyperlink in the WebView, I push another UITableView on the navigation stack: 在拦截WebView中超链接的onclick时,我将另一个UITableView推入导航堆栈:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL *url = [request URL];
    if (navigationType == UIWebViewNavigationTypeLinkClicked)
    {
        UIViewController *thirdViewController = [[MyTableViewController alloc] init];
        [self.navigationController pushViewController:thirdViewController animated:YES];

        return NO;
    }
    return YES;
}

As thirdViewController opens, its navigation bar only contains the back button with the title of the previous view to navigate back. 当thirdViewController打开时,其导航栏仅包含带有向后导航的上一个视图标题的后退按钮。 Its title and the right bar button are not shown... 其标题和右键栏按钮未显示。

This is how I try to display them: 这是我尝试显示它们的方式:

self.title = @"My Title";

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Push me" style:UIBarButtonItemStylePlain target:self action:@selector(push)];

I also tried setting the button through the navigationController, but still no luck: 我也尝试通过navigationController设置按钮,但是还是没有运气:

self.navigationController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Push me" style:UIBarButtonItemStylePlain target:self action:@selector(push)];

What could I be doing wrong here? 我在这里可能做错了什么?

Check that self.navigationItem and self.navigationController are not nil when accessed. 检查self.navigationItemself.navigationController在访问时是否不nil

From the documentation: 从文档中:

The first time the property is accessed, the UINavigationItem object is created. 第一次访问该属性时,将创建UINavigationItem对象。 Therefore, you shouldn't access this property if you are not using a navigation controller to display the view controller. 因此,如果不使用导航控制器显示视图控制器,则不应访问此属性。

A good place to access these members is under -viewDidLoad and not upon initialization. 一个访问这些成员的好地方是在-viewDidLoad下,而不是在初始化时。

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

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