2011-06-06 18 views
16

Estoy intentando esto pero no funciona.agregando más de dos botones en la barra de navegación

-(void)viewDidLoad 
{ 
    // create a toolbar where we can place some buttons 
    UIToolbar* toolbar = [[UIToolbar alloc] 
        initWithFrame:CGRectMake(0, 0, 100, 45)]; 
    [toolbar setBarStyle: UIBarStyleBlackOpaque]; 

    // create an array for the buttons 
    NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3]; 

    // create a standard save button 
    UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] 
     initWithBarButtonSystemItem:UIBarButtonSystemItemSave 
     target:self 
     action:@selector(saveAction:)]; 
    saveButton.style = UIBarButtonItemStyleBordered; 
    [buttons addObject:saveButton]; 
    [saveButton release]; 

    // create a spacer between the buttons 
    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] 
     initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace 
     target:nil 
     action:nil]; 
    [buttons addObject:spacer]; 
    [spacer release]; 

    // create a standard delete button with the trash icon 
    UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc] 
     initWithBarButtonSystemItem:UIBarButtonSystemItemTrash 
     target:self 
     action:@selector(deleteAction:)]; 
    deleteButton.style = UIBarButtonItemStyleBordered; 
    [buttons addObject:deleteButton]; 
    [deleteButton release]; 

    // put the buttons in the toolbar and release them 
    [toolbar setItems:buttons animated:NO]; 
    [buttons release]; 

    // place the toolbar into the navigation bar 
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] 
      initWithCustomView:toolbar]; 
    [toolbar release]; 
} 

¿Cómo puedo solucionar esto?

+0

hay varias preguntas similares en StackOverflow. Eche un vistazo a [estas preguntas] (http://stackoverflow.com/search?q=navigation+bar+multiple+items). – Piotr

+0

Lo sé, pero este código no funciona, ¿así es como preguntar? – Ajay

+0

esta es una pregunta de toque de cacao. así que por favor mantén la etiqueta. – vikingosegundo

Respuesta

11
UIToolbar* toolbar = [[UIToolbar alloc] 
         initWithFrame:CGRectMake(0, 0, 320, 45)]; 
[toolbar setBarStyle: UIBarStyleBlackOpaque]; 

// create an array for the buttons 
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:5]; 

// create a standard save button 
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] 
           initWithBarButtonSystemItem:UIBarButtonSystemItemSave 
           target:self 
           action:@selector(deleteAction:)]; 
saveButton.style = UIBarButtonItemStyleBordered; 
[buttons addObject:saveButton]; 
[saveButton release]; 

// create a standard delete button with the trash icon 
UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc] 
           initWithBarButtonSystemItem:UIBarButtonSystemItemTrash 
           target:self 
           action:@selector(deleteAction:)]; 
deleteButton.style = UIBarButtonItemStyleBordered; 
[buttons addObject:deleteButton]; 
[deleteButton release]; 

UIBarButtonItem *addbutton = [[UIBarButtonItem alloc] 
           initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
           target:self 
           action:@selector(deleteAction:)]; 
addbutton.style = UIBarButtonItemStyleBordered; 
[buttons addObject:addbutton]; 
[addbutton release]; 

UIBarButtonItem *editButton = [[UIBarButtonItem alloc] 
           initWithBarButtonSystemItem:UIBarButtonSystemItemEdit 
           target:self 
           action:@selector(deleteAction:)]; 
editButton.style = UIBarButtonItemStyleBordered; 
[buttons addObject:editButton]; 
[editButton release]; 

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] 
           initWithBarButtonSystemItem:UIBarButtonSystemItemDone 
           target:self 
           action:@selector(deleteAction:)]; 
doneButton.style = UIBarButtonItemStyleBordered; 
[buttons addObject:doneButton]; 
[doneButton release]; 

// put the buttons in the toolbar and release them 
[toolbar setItems:buttons animated:NO]; 
[buttons release]; 

// place the toolbar into the navigation bar 
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar]; 
[toolbar release]; 

probar este fragmento de código

Y En AppDelegate.h archivo que declara este

UINavigationController *navigationController; 

Y AppDelegate.m el archivo

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

// Override point for customization after application launch. 

// Add the view controller's view to the window and display. 
navigationController = [[UINavigationController alloc] initWithRootViewController:viewController]; 
[self.window addSubview:navigationController.view]; 
[self.window makeKeyAndVisible]; 

return YES; 
} 
+0

no es amigo. Solo quiero agregar el botón en la barra de navegación. Para hacer esto, simplemente elimino la barra de navegación en la vista y la dejo caer. Luego, después de la palabra, agregué este código en el archivo .m – Ajay

+1

He publicado un fragmento de código . En eso he agregado 5 botones. Echa un vistazo a esto. :) –

+0

¿Dónde debería escribir este código está en - (void) viewDidLoad {} – Ajay

2

Creo que me voy a dar u mi código esto va a resolver problema ur ...

-(void) initializeNavigationalBar { 

self.navigationItem.title = @"What Ever"); 

self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:(57/255) green:(57/255) blue:(57/255) alpha:1]; 
UIBarButtonItem *optionBtn; 
optionBtn = [[UIBarButtonItem alloc] 
      initWithImage:[UIImage imageNamed:@"image1.png"] 
      style:UIBarButtonItemStylePlain 
      target:self 
      action:@selector(LoadOption:)]; 
self.navigationItem.leftBarButtonItem = optionBtn; 

// create a toolbar to have two buttons in the right 
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 60, 44.01)]; 
// create the array to hold the buttons, which then gets added to the toolbar 
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:2]; 

UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] 
           initWithImage:[UIImage imageNamed:@"image2.png"] 
           style:UIBarButtonItemStylePlain target:self 
           action:@selector(decrement:)]; 

[buttons addObject:rightBtn]; 
[previousBtn release]; 
UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] 
          initWithImage:[UIImage imageNamed:@"image3.png"] 
          style:UIBarButtonItemStylePlain target:self 
          action:@selector(increment:)]; 
[buttons addObject:leftBtn]; 
[nextBtn release]; 
[tools setItems:buttons animated:NO]; 
tools.tintColor = [UIColor colorWithRed:(57/255) green:(57/255) blue:(57/255) alpha:1]; 
[buttons release]; 
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools]; 
[tools release];  

}

gustarán ...

+0

Gracias por el código. Pero estás haciendo una división entera, por lo que el botón siempre es negro. Simplemente cambie 255 por 255.0 y sería una división flotante. – Ricardo

0

funciona este código well.just ahora compruebo tu codigo. Build -> limpia todos los objetivos y después comprobar

+0

hola amigo .Puedo agregar segmento en el lugar del botón en la barra de navegación – Ajay

+0

@ user6271 Sí estoy seguro de agregar uisegmentedcontrol en la barra de navegación – Rams

+0

¿Cómo puedo reemplazar el botón de segmento1 con otro botón de segmento2 al hacer clic en el botón de segmento1? – Ajay

50

con iOS 5 puede agregar más botones

self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:uibarbuttonInstance1, uibarbuttonInstance2, nil]; 

mismo para los botones derecho

self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:uibarbuttonInstance1, uibarbuttonInstance2, nil]; 
+0

Gracias por la respuesta. Me ahorró mucho tiempo! – Varuna

+0

Sí ... definitivamente ahorró mucho tiempo ... +1 – Aditya

+0

+1. Gracias. ¡Fue rápido de implementar! – akshay1188

Cuestiones relacionadas