标题和条形按钮不显示在导航栏中

时间:2012-04-23 21:18:04

标签: iphone ios uitableview uinavigationbar title

设置标题并在导航栏中添加条形按钮时,我遇到了一些奇怪的困难。

我的应用是Tabbed应用。问题出在我的第一个标签中。第一个选项卡包含一个UINavigationController,它包含一个UITableViewController。来自我的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];

在firstViewController中选择一行时,我在导航堆栈上推送一个UIWebViewController:

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

在拦截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;
}

当thirdViewController打开时,其导航栏仅包含后退按钮,其中包含上一个视图的标题以进行导航。其标题和右侧栏按钮未显示......

这是我尝试显示它们的方式:

self.title = @"My Title";

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

我也试过通过navigationController设置按钮,但仍然没有运气:

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

我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

访问时,请检查self.navigationItemself.navigationController是否为nil

来自文档:

  

首次访问该属性时,将创建UINavigationItem对象。因此,如果未使用导航控制器显示视图控制器,则不应访问此属性。

访问这些成员的好地方是-viewDidLoad,而不是初始化。