开发者

Adding selector to navigationbar button programmatically

开发者 https://www.devze.com 2023-01-24 06:50 出处:网络
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:

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?


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;


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.


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];


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];


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

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

精彩评论

暂无评论...
验证码 换一张
取 消