如何将两个UIBarButtonItem添加到UINavigationItem?

时间:2010-06-09 17:25:07

标签: iphone objective-c cocoa-touch uinavigationcontroller

我想在我的UINavigationBar上有两个rightBarButtonItem。我怎么能做到这一点?

1 个答案:

答案 0 :(得分:7)

您可以使用带有两个按钮的UISegmentedControl,并将momentary属性设置为YES进行配置。

这是Mail应用程序中用于转到下一个/上一个消息的内容。

<强>更新

为了将UISegmentedControl] 1指定为右键,您必须将其包装在UIBarButtonItem中(取自NavBar sample application的示例代码):

- (void)viewDidLoad
{
    // "Segmented" control to the right
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
                                                [NSArray arrayWithObjects:
                                                    [UIImage imageNamed:@"up.png"],
                                                    [UIImage imageNamed:@"down.png"],
                                                 nil]];
    [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    segmentedControl.frame = CGRectMake(0, 0, 90, kCustomButtonHeight);
    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    segmentedControl.momentary = YES;

    UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
    [segmentedControl release];

    self.navigationItem.rightBarButtonItem = segmentBarItem;
    [segmentBarItem release];
}