以编程方式将选择器添加到导航栏按钮

发布于 2024-10-01 02:41:44 字数 326 浏览 2 评论 0原文

嘿,我正在尝试以编程方式向导航栏中的按钮添加选择器,但我似乎无法找出它根本不起作用的原因。这是我得到的代码:

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

我这样做是否正确?还有什么我应该做的吗?

编辑:我发现该按钮是nil,我不明白,因为它连接到rightBarButton插座。

编辑(再次):我发现导航项为零,知道问题出在哪里吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

花海 2024-10-08 02:41:44

我曾经这样做过并且工作正常

    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;

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;
心清如水 2024-10-08 02:41:44

所以你说 self.navigationController.navigationItem.rightBarButtonItem 是 nil,但是 rightBarButtonItem 在 IB 中是连接的。

造成这种情况的原因有两个:

  1. 您尚未加载笔尖。 (根据我对 UIViewController 的了解,这似乎不太可能。)
  2. navigationItemnavigationControllerselfnil

请记住,属性访问表达式实际上是消息表达式。例如,如果 self.navigationControllernil,则向其发送 navigationItem 消息将返回 nil。不管怎样,如果 self.navigationController.navigationItemnil,那么向其询问 rightBarButtonItem 将返回 nil< /代码>。

检查每一个子表达式 - selfself.navigationControllerself.navigationController.navigationItem - 并查看哪一个是 。然后,修复那个。

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.

泪痕残 2024-10-08 02:41:44

您还应该设置一个将接收操作的目标(我认为您将使用“self”):

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

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];
审判长 2024-10-08 02:41:44

只需尝试在 viewDidLoad 方法中以编程方式创建 rightBarButtonItem 即可:

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

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];
木落 2024-10-08 02:41:44

尝试在选择器名称后添加冒号,如下所示:

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

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

[self.navigationController.navigationItem.rightBarButtonItem setAction:@selector(showWithLabel:)];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文