2

Is it possible to add buttons to the navigation bar using the 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.

1 Answer 1

18

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");
      }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

by this method, is it possible to add more than 2 buttons to the navigation bar???
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.