1

Hey, I am trying to add a selector to a button in my navigationbar programmatically and I can't seem to find out why it doesn't work at all. Here is the code I got:

[self.navigationController.navigationItem.rightBarButtonItem setAction:@selector(showWithLabel)];

Am I doing this correctly and is there anything else I should do?

EDIT: I found out that the button was nil, I don't understand as it is connected to rightBarButton outlet.

EDIT(Again): I found out that the navigationitem is nil, any idea where is the problem then?

3
  • check whether the rightBarButtonItem is nil. Commented Nov 6, 2010 at 13:56
  • It was nil indeed, I don't have idea why as it is connected as rightbarbutton outlet ? Commented Nov 6, 2010 at 14:10
  • Shouldn't it be [self.navigationItem.rightBarButtonItem ... ? Commented May 1, 2012 at 15:29

5 Answers 5

2

I use to do that like this and works fine

    presetButton = [[UIButton alloc] initWithFrame:framePresetButton];
    UIImage *presetBtnBG = [[[UIImage imageNamed:@"segment_tools.png"] stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0] retain];


    [presetButton setImage:presetBtnBG forState:UIControlStateNormal];

    presetButtonItem = [[UIBarButtonItem alloc] initWithCustomView:presetButton];
    [presetButton addTarget:self action:@selector(loadPreset) forControlEvents:UIControlEventTouchUpInside];

    self.navigationItem.rightBarButtonItem = presetButtonItem;
Sign up to request clarification or add additional context in comments.

Comments

1

So you say self.navigationController.navigationItem.rightBarButtonItem is nil, but rightBarButtonItem is hooked up in IB.

There are two reasons why this could be:

  1. You haven't loaded your nib yet. (From what little I know of UIViewController, this seems unlikely.)
  2. navigationItem, navigationController, or self is nil.

Remember that property access expressions are really message expressions. If, say, self.navigationController is nil, then sending it the navigationItem message will return nil. If, one way or the other, self.navigationController.navigationItem is nil, then asking it for its rightBarButtonItem will return nil.

Check every one of those sub-expressions—self, self.navigationController, and self.navigationController.navigationItem—and see which one is nil. Then, fix that one.

1 Comment

Ok I found out that the navigation item is nil.
0

You should set also a target which will receive an action (I think you'll use 'self'):

[self.navigationController.navigationItem.rightBarButtonItem setAction:@selector(showWithLabel)];
[self.navigationController.navigationItem.rightBarButtonItem setTarget:self];

Comments

0

Just try to create rightBarButtonItem programmatically in viewDidLoad method:

UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editEventQuestion)]; 
self.navigationItem.rightBarButtonItem = rightBarButtonItem;
[rightBarButtonItem release];

Comments

0

Try adding a colon after your selector name, like this:

[self.navigationController.navigationItem.rightBarButtonItem setAction:@selector(showWithLabel:)];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.