2010-06-30 14 views

Respuesta

7

Utilice el código siguiente (nótese que necesita el "prev.png" y "imágenes" next.png - flechas):

- (void)addNextPrevSegmentedControl { 
    // Prepare an array of segmented control items as images 
    NSArray *nextPrevItems = [NSArray arrayWithObjects:[UIImage imageNamed:@"prev.png"], [UIImage imageNamed:@"next.png"], nil]; 
    // Create the segmented control with the array from above 
    UISegmentedControl* nextPrevSegmentedControl = [[UISegmentedControl alloc] initWithItems:nextPrevItems]; 
    [nextPrevSegmentedControl addTarget:self action:@selector(nextPrevAction:) forControlEvents:UIControlEventValueChanged]; 
    // Create the bar button item with the segmented control from above 
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:nextPrevSegmentedControl]; 
    // Add the bar button item from above to the navigation item 
    [self.navigationItem setRightBarButtonItem:rightButton animated:YES]; 
    // Release memory 
    [rightButton release]; 
    [nextPrevSegmentedControl release]; 
} 
- (void)nextPrevAction:(id)sender { 
    if ([sender isKindOfClass:[UISegmentedControl class]]) { 
     int action = [(UISegmentedControl *)sender selectedSegmentIndex]; 

     switch (action) { 
      case 0: 
       // Prev 
       break; 
      case 1: 
       // Next 
       break; 
     } 
    } 
} 


EDITAR: corrige el código

+0

Gracias por el código – Ameya

+1

'[nextPrevSegmentedControl setSegmentedControlStyle: UISegmentedControlStyleBar];' podría ser necesario, ya que estamos hablando de elementos de NavigationBar. –

+1

'[nextPrevSegmentedControl setMomentary: YES];' para permitir más de una pulsación de los botones. –

1

Se puede implementar utilizando un UISegmentedControl con 2 segmentos.

Establezca segmentedControlStyle como UISegmentedControlStyleBar.

Conjunto 2 UIImage para arriba y hacia abajo.

+0

Gracias para el código – Ameya

Cuestiones relacionadas