UINavigationController工具栏:问题设置UIBarButtonItems

时间:2010-09-07 18:27:24

标签: iphone objective-c toolbar uibarbuttonitem

 -(void)viewWillAppear:(BOOL)animated {
 //setup toolbar
 [self.navigationController setToolbarHidden:NO];
 self.navigationController.toolbar.barStyle = UIBarStyleBlackTranslucent;
 self.navigationController.toolbar.alpha = .8;
 self.navigationController.toolbar.tintColor = [UIColor colorWithRed:6.0/255.0 green:95.0/255.0 blue:163.0/255.0 alpha:1];

 //setup items
 UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self.navigationController.toolbar action:nil];
 UIImage *imageUp = [UIImage imageNamed:@"up.png"];
 UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithImage:imageUp style:UIBarButtonItemStylePlain target:self action:@selector(toggleUp:)];
 item2.width = 5;
 UIImage *imageDown = [UIImage imageNamed:@"down.png"];
 UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
 UIBarButtonItem *item4 = [[UIBarButtonItem alloc] initWithImage:imageDown style:UIBarButtonItemStylePlain target:self action:@selector(toggleDown:)];
 UIBarButtonItem *item5 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
 UIBarButtonItem *item6 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(toggleDown:)];
 UIBarButtonItem *item7 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];


 NSArray *itemsArray = [[NSArray alloc] initWithObjects:item1, item2, item3, item4, item5, item6, item7, nil ];


 [self setToolbarItems:itemsArray];


...more

} 

我不确定这里发生了什么,我已经阅读了很多关于如何为工具栏做的事情,但我似乎无法显示任何UIBarButtonItems。

有什么想法吗?

1 个答案:

答案 0 :(得分:4)

对不起上面乱糟糟的代码。

我找到了问题的答案。 我正在访问UINavigationController的一个孩子,而不是根UIView。 我将上面的代码分配给viewWillAppear到我的UINavigationController的根UIViewController,它工作正常。所以要仅在我的子UIViewController上显示它我必须设置

[self.navigationController setToolbarHidden:NO];

对于我不希望工具栏可见的所有UIViewControllers。

希望这可以帮助别人。感谢