Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding left button to UINavigationBar (iPhone)

I've created a new navigation based iPhone app. I added this to the RootViewController.

- (void)viewDidLoad {     [super viewDidLoad];     UIBarButtonItem *addButton = [[UIBarButtonItem alloc] init]; self.navigationItem.leftBarButtonItem = addButton; self.navigationItem.leftBarButtonItem.enabled = YES; } 

No left button displays however. Is there something I need to do?

like image 791
4thSpace Avatar asked Feb 19 '09 16:02

4thSpace


People also ask

How do I add a button to my navigation bar?

Example ExplainedUse any element to open the dropdown menu, e.g. a <button>, <a> or <p> element. Use a container element (like <div>) to create the dropdown menu and add the dropdown links inside it. Wrap a <div> element around the button and the <div> to position the dropdown menu correctly with CSS.


2 Answers

You don't define what the button actually does. This is a line from my app:

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelEdit:)]; 

cancelEdit:, the selector, is in the current class (self) and is defined as:

- (void) cancelEdit: (id) sender; 
like image 113
Stephen Darlington Avatar answered Sep 18 '22 17:09

Stephen Darlington


There is actually another answer, which is not listed here, but might be very useful in many cases. If you do not want to use UINavigationController, then self.navigationItem is not an option for you.

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"imageName"] style:UIBarButtonItemStyleBordered target:self action:@selector(action:)]; UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:@"Bar Title"]; navigationItem.leftBarButtonItem = barButton; [navigationBar pushNavigationItem:navigationItem animated:NO]; 

You might want that when creating lightweight UIViewController with bar and buttons, but do not want navigation overhead.

like image 43
Roman B. Avatar answered Sep 20 '22 17:09

Roman B.