views:

173

answers:

2

I want two rightBarButtonItem's on my UINavigationBar. How can I accomplish this?

+1  A: 

You can use a UISegmentedControl with two buttons and configure it with the momentary property set to YES.

This is what is used in the Mail application to go to next/previous message.

Update

In order to assign the UISegmentedControl]1 as a right button, you have to wrap it inside a UIBarButtonItem (sample code taken from the 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];
}
Laurent Etiemble
Can a UISegmentedControl be casted as UIBarButtonItem?
Sheehan Alam
I have update my answer to show how to use an UISegmentedControl as rightBarButtonItem.
Laurent Etiemble
A: 

Great... I was looking for this for a long time.

Thank you!!

Koichi