Adding buttons to navigation bar

2020-06-06 07:11发布

问题:

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:

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");
      }
    }