0

I am unable to set the left button in my navigation bar. Anyone have an idea why the code below is not displaying a button?

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] 
 initWithTitle:@"my left button" 
 style:UIBarButtonItemStyleBordered 
 target:nil 
 action:nil];


[self.navigationItem setLeftBarButtonItem:barButton];

 //this is now printing out "my left button", 
 //but the button still does not appear on the navigation.
 NSLog(@"navigationItem.leftBarButtonItem.title:
  %@",self.navigationItem.leftBarButtonItem.title);

Interface Builder:

enter image description here

Simulator:

enter image description here

---UPDATE----

The above code is updated with correct enum and init based on CodaFi's suggestions but the button is still not appearing.

joe
  • 16,988
  • 36
  • 94
  • 131
  • What is navigationItem and where have you defined and initialized it? – trumpetlicks Jun 18 '12 at 19:30
  • The posted code is in the viewDidLoad of my subclass of a UIViewController. navigationItem is a property of UIViewController, do I need to initialize it? – joe Jun 18 '12 at 19:34

1 Answers1

1

The initWithBarButtonSystemItem: part of your init method is being supplied the wrong enum value.

System items are different from bar button styles (case in point UIBarButtonItem*Style*Bordered, vs UIBarButton*System*ItemAdd).

Here is the list of valid enum values:

typedef enum { UIBarButtonSystemItemDone, UIBarButtonSystemItemCancel, UIBarButtonSystemItemEdit, UIBarButtonSystemItemSave, UIBarButtonSystemItemAdd, UIBarButtonSystemItemFlexibleSpace, UIBarButtonSystemItemFixedSpace, UIBarButtonSystemItemCompose, UIBarButtonSystemItemReply, UIBarButtonSystemItemAction, UIBarButtonSystemItemOrganize, UIBarButtonSystemItemBookmarks, UIBarButtonSystemItemSearch, UIBarButtonSystemItemRefresh, UIBarButtonSystemItemStop, UIBarButtonSystemItemCamera, UIBarButtonSystemItemTrash, UIBarButtonSystemItemPlay, UIBarButtonSystemItemPause, UIBarButtonSystemItemRewind, UIBarButtonSystemItemFastForward, UIBarButtonSystemItemUndo, // iOS 3.0 and later UIBarButtonSystemItemRedo, // iOS 3.0 and later UIBarButtonSystemItemPageCurl

CodaFi
  • 43,043
  • 8
  • 107
  • 153
  • What enum value should be passed in? – joe Jun 18 '12 at 19:51
  • I just changed it to UIBarButtonItemStyleBordered and the log is still printing null – joe Jun 18 '12 at 19:54
  • Yeah, that's the point. It's a style, not a system item. You're using the wrong init method then. Use initWithTitle:style:target:action: instead for style – CodaFi Jun 18 '12 at 19:56
  • The button is still not appearing. Do I need to somehow link my Navigation Bar or Navigation Item from my xib file to my implementation file? – joe Jun 18 '12 at 20:05
  • I've never heard of someone making a Navigation controller in a XIB. update your question with what you just tried. – CodaFi Jun 18 '12 at 20:07
  • I have updated my question and added screenshots. Thanks for the help. – joe Jun 18 '12 at 20:10
  • I SEE! You have made a nav bar in IB which is not automatically set to self.navgationController, so the real navigation controller that you've been trying to add a bar item to doesn't exist! You need to do one of two things: write a nav bar in code in the App Delegate and delete the XIB one, or make a reference to the XIB nav bar and add the button to it. – CodaFi Jun 18 '12 at 20:17
  • ahhh okay... How do I make a reference to the XIB nav bar? – joe Jun 18 '12 at 20:30
  • Make an IBOutlet for one. That's your new nav controller. Beware, though, the controller has relevance only within this view controller. Declare one in the app delegate if you need to go deeper than one level with a Push. – CodaFi Jun 18 '12 at 20:31
  • Okay, thanks. I am not pushing at all, this VC is displayed in a modal using presentModalViewController. – joe Jun 18 '12 at 20:39
  • Excellent. Happy coding. And if you wouldn't mind up voting and accepting? – CodaFi Jun 18 '12 at 20:41
  • Thanks, UINavigationBar does not have the method setLeftBarButtonItem. So I am not sure what to use. – joe Jun 18 '12 at 20:58
  • I think this is what I need for that http://stackoverflow.com/questions/2504351/how-to-add-a-button-to-uinavigationbar – joe Jun 18 '12 at 21:00
  • Yes, exactly. UINavigationItem is an annoying class to use, but it's necessary in your case. – CodaFi Jun 18 '12 at 21:02
  • Maybe I am going about it the wrong way. Do I need to use UINavigationItem in order to have custom buttons? – joe Jun 18 '12 at 21:11
  • Yes. If you were to set this up in the app delegate; then an internal UINavigationItem would have been set up for you, but because you are using a XIB, you must define your own. It'll work great. – CodaFi Jun 18 '12 at 21:14