1

I have a table view in a Navigation Controller, with a Navigation Bar at the top. I would like to add a plus button on the right of this navigation bar, and for the button to post an NSLog when pressed. However, all online resources suggesting to add the navigation bar programatically have failed. How can I do this?

All help appreciated.

Edit: Here is some code I used in my viewDidLoad method. So that you know, I simply added this code, and did nothing else:

UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStyleDone target:self action:@selector(doSave:)];
self.navigationItem.rightBarButtonItem = anotherButton;
[anotherButton release];  

Edit 2: When I created the project, in the Interface Builder, I created a Navigation Controller, moved the arrow which was to the left of my FirstViewController to the Navigation Controller, then deleted my FirstViewController. Would this have stopped the code from working?

Isaac A
  • 361
  • 2
  • 24
  • Post some code of what you have tried--chances are the easiest fix is some small bug in an implementation that you've already tried. – Morgan Chen Nov 16 '14 at 20:33

2 Answers2

2
- (void)viewDidLoad {
    [super viewDidLoad];
     
    UIBarButtonItem *plusButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(plusButtonHit)];
    self.navigationItem.rightBarButtonItem = plusButton;
}

- (void)plusButtonHit {
    NSLog(@"Log something");
}
pkamb
  • 33,281
  • 23
  • 160
  • 191
0

In your ViewController's viewDidLoad:

UIBarButtonItem *plusButton = [[UIBarButtonItem alloc]initWithTitle:@"+" style:UIBarButtonItemStylePlain target:self action:@selector(plusButtonHit)];
self.navigationItem.rightBarButtonItem = plusButton;

-(void)plusButtonHit {
   // do something
   NSLog(@"Log something");
}

If you want to use an image, just create your bar button with an image instead of text.

stromdotcom
  • 381
  • 1
  • 8
  • 1
    I tried this, but it did nothing :( http://i.imgur.com/gzZXYXp.png - I added a part below my edit in the main post - Do you think this would have caused my code to not be working? – Isaac A Nov 16 '14 at 20:40
  • Did you set the class for your view controller in interface builder? Click the first icon at the top (ViewController), on the right select the identity inspector, and under class set it to your view controller. – stromdotcom Nov 16 '14 at 20:44
  • It hasn't been set to ViewController; however, when I type in 'ViewController', and hit enter, it doesn't do anything. I deselect, and select again, and the text is gone. If I do this with a new View Controller, however, the Class can be set with no problem at all :| – Isaac A Nov 16 '14 at 20:47
  • Well there's your problem. My code works fine, but the ViewController has no idea it is of type **ViewController**. – stromdotcom Nov 16 '14 at 20:52
  • Thanks a lot! Before I leave, would you have any idea why the drop-down menu isn't working? I've tried deleting derived data, cleaning the project, etc, but I can't actually set View Controller as it's custom class, nor any other class for that matter. The menu works fine for a single View Controller, however :\ – Isaac A Nov 16 '14 at 20:58
  • Never mind, fixed! Had to change the line of code in my .h file to '@interface ViewController : UITableViewController'. Thanks a lot for your help! – Isaac A Nov 16 '14 at 21:04