添加按钮到导航栏

时间:2009-12-14 11:05:36

标签: iphone navigation uinavigationbar

是否可以使用IPhone SDK将按钮添加到导航栏?

导航栏中已有2个按钮,分别为leftBarButton和rightBarButton。我还需要2个按钮。如何实现?

我不需要将它们包含在导航栏中。但由于应用程序只包含一个表,我不认为它可以在其他地方给出。

1 个答案:

答案 0 :(得分:18)

您可以使用UISegmentedControl。检查UICatalog代码示例,在导航栏中查看其用法。

以下是一些示例代码:

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