【问题标题】:Title and bar button not displaying in navigation bar标题和栏按钮未显示在导航栏中
【发布时间】:2012-04-23 21:18:04
【问题描述】:

我在设置标题和向导航栏添加栏按钮时遇到了一些奇怪的困难。

我的应用程序是一个选项卡式应用程序。问题出在我的第一个标签中。第一个选项卡包含一个 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)];

我在这里做错了什么?

【问题讨论】:

    标签: iphone ios uitableview uinavigationbar title


    【解决方案1】:

    检查self.navigationItemself.navigationController 在访问时不是nil

    来自文档:

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

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

    【讨论】:

    • 谢谢!我已经在我的应用程序上工作了一段时间,但有时仍然会为将我的初始化内容放在哪里而苦苦挣扎。将我的代码移动到 -viewDidLoad ,它就像一个魅力:)
    • 有时它仍然发生在我身上。虽然self.title = @"Something"; 不起作用,但很奇怪,因为放在 -init 中也应该起作用..
    • 这让我一直想知道,因为我在我的其他视图控制器中也是这样做的,而且不知何故,它只是出现了这个问题......
    猜你喜欢
    • 1970-01-01
    • 2019-01-11
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 2014-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多