Adding left button to UINavigationBar (iPhone)
Asked Answered
P

3

27

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?

Penchant answered 19/2, 2009 at 16:3 Comment(0)
B
55

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;
Backandforth answered 19/2, 2009 at 16:7 Comment(4)
Awesome, thanks. Where do you find the various selectors available? The doc is very vague about this. I defined an instance method and put that as the selector but it was never executed. I'd like to slide in a detailview when the button is clicked.Penchant
The selector is defined in the class (I've added that to my answer).Backandforth
I've also found out how to do this through IB. It's added as a Navigation Item but not through the RootViewController.xib where the tableview is. It has to be added to the MainWindow.xib.Penchant
It's missing an autorelease, to remove the existing memory leak: self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelEdit:)] autorelease];Nautilus
R
14

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.

Rosinweed answered 7/10, 2012 at 21:35 Comment(0)
D
11

On this question:

Awesome, thanks. Where do you find the various selectors available? The doc is very vague about this. I defined an instance method and put that as the selector but it was never executed. I'd like to slide in a detailview when the button is clicked. – 4thSpace Feb 19 at 16:19

I go the place where I need more information and press the escape (Esc) key. So, in this example:

...(beginning of line)... @selector(Place Cursor Here, press Esc) ...

A list of the available selectors will appear. For Microsoft programmers, this is like Intellisense, but you have to ask for it with Esc (it just doesn't appear automatically like in Visual Studio). Practically speaking, XCode does create most of whatever you're trying to create when you start typing and it really helps when you figure out the Tab key is your friend. (well... it's my friend... having the lonely life I have)

Now, if you need your own selector, you can place your label in there (mySelector: for example), then, further down in your code, build it:

- (IBAction)mySelector:(id)sender {
NSLog(@"You touched me THERE!");
}

Also, in the header (.h) file, be sure to put a corresponding:

-(IBAction) mySelector:(id)sender;

Denote answered 6/9, 2009 at 4:3 Comment(3)
Thanks for the escape key tip! I was wondering about that. :)Mcgruter
BTW - Control-comma will present a list that you can scroll through.Reginareginald
Nevermind -- I just tried the ESC and it is the same as my Control-comma and a lot easier to hit quickly! Your comment is better ... :)Reginareginald

© 2022 - 2024 — McMap. All rights reserved.