Ios setItems中的空白工具栏

Ios setItems中的空白工具栏,ios,uitoolbar,Ios,Uitoolbar,我的UINavigationController工具栏为空,没有显示任何项目。这是我的密码 self.navigationController.toolbarHidden = NO; UIToolbar* toolbar = self.navigationController.toolbar; NSMutableArray *items = [[NSMutableArray alloc] init]; [items addObject:[[UIBarButtonItem alloc] init

我的UINavigationController工具栏为空,没有显示任何项目。这是我的密码

self.navigationController.toolbarHidden = NO;

UIToolbar* toolbar = self.navigationController.toolbar;
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:[[UIBarButtonItem alloc] initWithTitle:@"Test"
                                                  style:UIBarButtonItemStylePlain
                                                 target:self
                                                 action:@selector(buttonPushed)]];
[toolbar setItems:items animated:YES];

这不是设置工具栏的正确方法
UIViewController
具有
toolbarItems
属性。导航控制器将使用该属性自动填充其工具栏

您的代码应该是:

self.navigationController.toolbarHidden = NO;

UIBarButtonItem *btnTest = [[UIBarButtonItem alloc] initWithTitle:@"Test"
                                              style:UIBarButtonItemStylePlain
                                             target:self
                                             action:@selector(buttonPushed)]];
self.toolbarItems = @[ btnTest ];

谢谢。这对我有用。如何扩展它以添加任意数量的按钮?