views:

2025

answers:

1

Is it possible to add buttons to the navigation bar in iphone sdk? I already have 2 buttons in the navigation bar as leftBarButton and rightBarButton. I need 2 more buttons. How to implement that? Its not obligatory that i need them to be included in the navigation bar itself. But since the application contains only a table, i don't think it can be given elsewhere.. pls help

+4  A: 

You can use the UISegmentedControl. Check the UICatalog code sample to check its usage in the navigation bar.

Here is some sample code:

       - (void)viewDidLoad {
[super viewDidLoad];

 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, 35);
      segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
      segmentedControl.momentary = YES;

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

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

    - (void)segmentAction:(id)sender{

      if([sender selectedSegmentIndex] == 0){
       //do something with segment 1
       NSLog(@"Segment 1 preesed");
      }else{
       //do something with segment 2
       NSLog(@"Segment 2 preesed");
      }
    }
Benjamin Ortuzar
by this method, is it possible to add more than 2 buttons to the navigation bar???
Nithin
You can add several segments in which you can perform actions as you need. Check the UICatalog code sample, and you will see it working in the iphone simulator with two options.
Benjamin Ortuzar