我正在尝试动态创建一个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];
答案 0 :(得分:8)
更改
navigationController.navigationItem.leftBarButtonItem = closeButton;
到
tempViewController.navigationItem.leftBarButtonItem = closeButton;
navigationItem
外观由导航控制器显示的视图处理,因此如果您在UINavigationController
上设置但不在rootViewController
上设置,则在{{1}}时将其覆盖你第一次推它。
答案 1 :(得分:1)
您需要访问tempViewController的navigationItem,而不是navigationController。