2011-05-25 23 views
7

que estaba trabajando con el botón de la barra de navegación items.i estaba usando el siguiente código para hacerloAdición del artículo botón de la barra en la barra de navegación

UIBarButtonItem *btnSave = [[UIBarButtonItem alloc] 
            initWithTitle:@"Save"            
            style:UIBarButtonItemStyleBordered 
            target:self 
           action:@selector(save_Clicked:)]; 
    self.navigationItem.rightBarButtonItem = btnSave; 
    [btnSave release]; 

    UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] 
            initWithTitle:@"Cancel"            
            style:UIBarButtonItemStyleBordered 
            target:self 
            action:@selector(save_Clicked)]; 
    self.navigationItem.leftBarButtonItem = btnCancel; 
    [btnCancel release]; 

mi pregunta es cómo agregar otro botón justo al lado del botón de la barra izquierda ít. gracias de antemano

Respuesta

8

Para hacer esto es necesario crear una barra de herramientas a continuación, seguir añadiendo UIButton a ella, a continuación, establecer la barra de herramientas como el leftBarButton

algo como esto:

UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 250, 44)]; 
tools.tintColor = [UIColor clearColor]; 
[tools setTranslucent:YES]; 

NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:9]; 

UIImage *myImage = [UIImage imageNamed:@"AL_HomeMod_Icon.png"]; 
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[myButton setImage:myImage forState:UIControlStateNormal]; 
myButton.showsTouchWhenHighlighted = YES; 
myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height); 

[myButton addTarget:self action:@selector(clickViewHomeMod) forControlEvents:UIControlEventTouchUpInside]; 

UIBarButtonItem *bi = [[UIBarButtonItem alloc] 
         initWithCustomView:myButton]; 

[buttons addObject:bi]; 
[bi release]; 

myImage = [UIImage imageNamed:@"AL_History_Icon.png"]; 
myButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[myButton setImage:myImage forState:UIControlStateNormal]; 
myButton.showsTouchWhenHighlighted = YES; 
myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height); 

[myButton addTarget:self action:@selector(clickViewHistory) forControlEvents:UIControlEventTouchUpInside]; 

bi = [[UIBarButtonItem alloc] 
     initWithCustomView:myButton]; 

[buttons addObject:bi]; 
[bi release]; 

myImage = [UIImage imageNamed:@"AL_RX_Icon.png"]; 
myButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[myButton setImage:myImage forState:UIControlStateNormal]; 
myButton.showsTouchWhenHighlighted = YES; 
myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height); 

[myButton addTarget:self action:@selector(clickViewCustomPopView2) forControlEvents:UIControlEventTouchUpInside]; 

bi = [[UIBarButtonItem alloc] 
     initWithCustomView:myButton]; 

[buttons addObject:bi]; 
[bi release]; 

myImage = [UIImage imageNamed:@"AL_User_Icon.png"]; 
myButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[myButton setImage:myImage forState:UIControlStateNormal]; 
myButton.showsTouchWhenHighlighted = YES; 
myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height); 

[myButton addTarget:self action:@selector(clickViewCustomPopView:) forControlEvents:UIControlEventTouchUpInside]; 
bi = [[UIBarButtonItem alloc] 
     initWithCustomView:myButton]; 
[buttons addObject:bi]; 
popButton = myButton; 
[bi release]; 


// stick the buttons in the toolbar 
[tools setItems:buttons animated:NO]; 

[buttons release]; 

// and put the toolbar in the nav bar 
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools]; 
[tools release]; 

esperanza de que ayude

Pondd

1

Cree una vista personalizada con dos botones y use UIBarButtonItem 's initWithCustomView: inicializador. Deberias hacer eso.

2

Logré mi tarea utilizando el siguiente código:

UIToolbar *tools=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 150, 44)]; 

tools.backgroundColor=[UIColor clearColor]; 

[tools setTranslucent:YES]; 

UIBarButtonItem *optionBtn=[[UIBarButtonItem alloc]initWithTitle:@"Options" style:UIBarButtonItemStyleBordered target:self action:@selector(optionPressed:)]; 

UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(donePressed:)]; 

NSArray *buttons=[NSArray arrayWithObjects:optionBtn,doneBtn, nil]; 

    [tools setItems:buttons animated:NO]; 

self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:tools]; 

NOTA: A partir de iOS 5.0 en adelante, Apple ha hecho mucho más fácil. Se puede hacer como

self.navigationItem.rightBarButtonItems=[NSArray arrayWithObjects:optionBtn,doneBtn, nil]; 
7

Crear un botón como

UIBarButtonItem *logoutButton = [[UIBarButtonItem alloc] 
           initWithImage:[UIImage imageNamed:@"logout.png"] 
           style:UIBarButtonItemStylePlain 
           target:self action:@selector(doLogout)]; 

Añadir este botón para la derecha de la barra de navegación

self.navigationItem.rightBarButtonItem = logoutButton; 

o añadir este botón a la izquierda de la barra de navegación

self.navigationItem.leftBarButtonItem = logoutButton; 

doLogout es una función que se ejecutará al tocar el botón de cierre de sesión

Cuestiones relacionadas