2012-05-30 146 views
12

我正在使用一个故事板,并且我有一个拆分视图,其中主人是UITableViewController。就像iPad Mail应用程序一样,我想显示一个UIToolbar。UIToolbar不显示UIBarButtonItem

我无法通过故事板添加工具栏,但我设法以编程方式添加它。我也可以添加一个UILabel到工具栏,但我找不到添加刷新按钮或任何种类的UIBarButtonItem的方法。

有什么想法?

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    [self.navigationController setToolbarHidden:NO]; 

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50.0f, 0.0f, 80.0f, 40.0f)]; 
    label.text = @"last updated..."; 
    label.textAlignment = UITextAlignmentCenter; 
    label.font = [UIFont systemFontOfSize:13.0]; 
    [self.navigationController.toolbar addSubview:label]; 

    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"Item" style:UIBarButtonItemStylePlain target:self action:@selector(action:)]; 
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"Item1" style:UIBarButtonItemStyleBordered target:self action:@selector(action:)]; 

    NSArray *buttons = @[item1, item2, nil]; 
    [self.navigationController.toolbar setItems:buttons animated:NO]; 

回答

39

找到答案感谢苹果iOS论坛!

当您使用导航控制器的工具栏时,必须在视图控制器toolbarItems属性上设置工具栏按钮,而不是在实际工具栏本身上。

例如:

[self setToolbarItems:buttons animated:NO]; 
+0

ü救了我的天!.... Thanksss – sheetal

+0

妈的,谢谢!这节省了时间!谢谢! – yohannes