Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a right button to a UINavigationController?

I am trying to add a refresh button to the top bar of a navigation controller with no success.

Here is the header:

@interface PropertyViewController : UINavigationController {  } 

Here is how I am trying to add it:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {     if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {         UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain                                           target:self action:@selector(refreshPropertyList:)];               self.navigationItem.rightBarButtonItem = anotherButton;     }     return self; } 
like image 940
Artilheiro Avatar asked Aug 02 '09 20:08

Artilheiro


People also ask

How do I add a button to my navigation bar?

Use 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.

What is navigationcontroller?

A navigation controller is a container view controller that manages one or more child view controllers in a navigation interface. In this type of interface, only one child view controller is visible at a time.


1 Answers

Try doing it in viewDidLoad. Generally you should defer anything you can until that point anyway, when a UIViewController is inited it still might be quite a while before it displays, no point in doing work early and tying up memory.

- (void)viewDidLoad {   [super viewDidLoad];    UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain target:self action:@selector(refreshPropertyList:)];             self.navigationItem.rightBarButtonItem = anotherButton;   // exclude the following in ARC projects...   [anotherButton release]; } 

As to why it isn't working currently, I can't say with 100% certainty without seeing more code, but a lot of stuff happens between init and the view loading, and you may be doing something that causes the navigationItem to reset in between.

like image 58
Louis Gerbarg Avatar answered Sep 28 '22 01:09

Louis Gerbarg