添加的按钮不会显示在UINavigationController上

时间:2013-03-28 18:24:28

标签: objective-c cocoa-touch uiviewcontroller

我正在尝试动态创建一个viewcontroller并设置其视图和标题栏。事情似乎工作得很好。但是当我尝试设置导航栏的左按钮或右按钮时,它们就不会显示出来。我做错了什么?

   UIWebView *tempWebView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    [tempWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"someurl"]]];

    UIViewController* tempViewController = [[UIViewController alloc]init];
    tempViewController.view = tempWebView;

    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:tempViewController];

    UIBarButtonItem* closeButton = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStyleBordered target:self action:nil];
    navigationController.navigationItem.leftBarButtonItem = closeButton;


    [self presentViewController:navigationController animated:YES completion:nil];

2 个答案:

答案 0 :(得分:8)

更改

navigationController.navigationItem.leftBarButtonItem = closeButton;

tempViewController.navigationItem.leftBarButtonItem = closeButton;

navigationItem外观由导航控制器显示的视图处理,因此如果您在UINavigationController上设置但不在rootViewController上设置,则在{{1}}时将其覆盖你第一次推它。

答案 1 :(得分:1)

您需要访问tempViewController的navigationItem,而不是navigationController。