iphone – 设置navigationController的工具栏项

我正在尝试使用以下调用为navigationController设置项目

NSArray *items = [NSArray arrayWithObjects: shareButton, nil];
[self.navigationController.toolbar setItems:items animated:NO];

这对工具栏没有任何补充.

我可以使用隐藏和显示工具栏

[self.navigationController setToolbarHidden:NO];

但是不能让物品出现.

如何设置项目. ?

更新:

最佳答案
工具栏是只读属性.您可以通过以下方式设置工具栏:

toolbar The custom toolbar associated
with the navigation controller.
(read-only)

@property(nonatomic,readonly)
UIToolbar *toolbar Discussion This
property contains a reference to the
built-in toolbar managed by the
navigation controller. Access to this
toolbar is provided solely for clients
that want to present an action sheet
from the toolbar. You should not
modify the UIToolbar object directly.

Management of this toolbar’s contents
is done through the custom view
controllers associated with this
navigation controller. For each view
controller on the navigation stack,
you can assign a custom set of toolbar
items using the
setToolbarItems:animated: method of
UIViewController.

编辑:所以你应该这样做:

[self setToolbarItems:items animated:NO];

编辑:这是如何添加正确的栏按钮项:

- (void) addRightButton
{
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setImage:[UIImage imageNamed:@"mybutton.png"] forState:UIControlStateNormal];
    rightBtn.frame = CGRectMake(0, 0, 70, 40 );
    [rightBtn addTarget:self action:@selector(myButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightBarBtn;
}

编辑:以编程方式创建灵活/固定空间项目,使用此:

- (id)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem target:(id)target action:(SEL)action

可设置的systemItem值包括UIBarButtonSystemItemFlexibleSpace和UIBarButtonSystemItemFixedSpace.查看UIBarButtonItem类的文档:

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIBarButtonItem_Class/Reference/Reference.html

编辑:问题澄清了.底部的工具栏与navigationItem或导航控制器无关,它只是一个UIToolbar.您需要在IB中完全设置它或在您的类中设置一个插座并在代码中设置/完成它.

转载注明原文:iphone – 设置navigationController的工具栏项 - 代码日志