Posted By

CharlyBr on 06/14/10


Tagged

UIButton UINavigationBar UIToolbar


Versions (?)

Who likes this?

8 people have marked this snippet as a favorite

umang_nine
bobbym245
sb
nsbuckner
barneymc
wpellis
Tintenklecks
cnathan


Add 2 UIButtons to a UINavigationBar right item


 / Published in: Objective C
 

  1. // create a toolbar to have two buttons in the right
  2. UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 90.0, 44.01)];
  3.  
  4. // create the array to hold the buttons, which then gets added to the toolbar
  5. NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:2];
  6.  
  7. // create a standard "add" button
  8. UIButton *btnAdd = [[UIBarButtonItem alloc]
  9. initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add)];
  10. [buttons addObject:btnAdd];
  11. [btnAdd release];
  12.  
  13. // create a "edit" button
  14. UIButton *btn = [[UIBarButtonItem alloc]
  15. initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered target:self action:@selector(edit)];
  16. [buttons addObject:btnEdit];
  17. [btnEdit release];
  18.  
  19. // stick the buttons in the toolbar
  20. [tools setItems:buttons animated:NO];
  21. [buttons release];
  22.  
  23. // and put the toolbar in the nav bar
  24. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
  25. [tools release];

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: mr200backstrok on July 3, 2010

line 14.: change *btn to *btnEdit

or else there will be a compiler error at line 16.

You need to login to post a comment.