ios 以编程方式创建的 UIBarButtonItem 不启动选择器操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6669520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:43:08  来源:igfitidea点击:

Programmatically created UIBarButtonItem Not Launching Selector Action

objective-ciosxcodeuinavigationcontrolleruibarbuttonitem

提问by ArtSabintsev

Here is my UIBarButton:

这是我的UIBarButton

[self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] 
                            initWithTitle:@"+ Contact" 
                                    style:UIBarButtonItemStylePlain 
                                   target:nil 
                                   action:@selector(showPicker:)] animated:YES];

Here is the code it's supposed to launch:

这是它应该启动的代码:

- (void)showPicker:(id)sender {
    ABPeoplePickerNavigationController *picker = 
     [[ABPeoplePickerNavigationController alloc] init];
    picker.peoplePickerDelegate = self;

    [self presentModalViewController:picker animated:YES];
    [picker release];
}

When I launch the app and click on the '+ Contact' UIBarButton, nothing happens. No errors, nada. I put in a breakpoint, and it never reaches the method referenced by the selector.

当我启动应用程序并单击“+ 联系人”时UIBarButton,没有任何反应。没有错误,纳达。我设置了一个断点,它永远不会到达选择器引用的方法。

Am I doing something wrong in the way I'm calling the selector?

我在调用选择器的方式上做错了吗?

Thanks!

谢谢!

回答by ageektrapped

The declaration of your button is missing something, namely the targetparameter. Try this:

按钮的声明缺少一些东西,即target参数。尝试这个:

UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"+ Contact" 
                                 style:UIBarButtonItemStylePlain 
                                target:self 
                                action:@selector(showPicker:)];
[self.navigationItem setLeftBarButtonItem:item animated:YES];

This assumes that showPicker:is in fact in the same class that's adding the button to the navigation item.

这假设showPicker:实际上是在将按钮添加到导航项的同一个类中。

The targetparameter is the instance that should handle the event.

target参数是要处理事件的实例。

回答by w3bshark

For those that are still having trouble with this, here is another solution that I have found: Instead of doing this:

对于那些仍然有问题的人,这是我发现的另一个解决方案:而不是这样做:

self.myBarButton = 
  [[UIBarButtonItem alloc] initWithTitle:@"Woot Woot"
                                   style:UIBarButtonItemStyleBordered 
                                  target:self 
                                  action:@selector(performActionForButton)];

Try something like this:

尝试这样的事情:

NSArray *barButtons = [self.myToolbar items];
UIBarButtonItem *myBarButton = [barButtons objectAtIndex:0];
[myBarButton setAction:@selector(performActionForButton)];

*Make sure you've added this UIBarButtonItemin the toolbar in Storyboard. (Or you could just programmatically create your own UIBarButtonItembefore this set of code and add it to the itemsarray of the UIToolbar.)

*确保您已将其添加UIBarButtonItem到 Storyboard 的工具栏中。(或者您可以UIBarButtonItem在这组代码之前以编程方式创建自己的代码并将其添加到UIToolbar的items数组中。)

Somehow, ageektrapped's solution did not work for me, although his solution is what I would rather use. Maybe someone more knowledgeable about UIBarButtonItems could comment on why one solution worked over the other?

不知何故,ageektrapped的解决方案对我不起作用,尽管他的解决方案是我宁愿使用的。也许对 UIBarButtonItems 更了解的人可以评论为什么一种解决方案优于另一种解决方案?

回答by tomk

The "target" should be the object the selector belongs to, instead of nil.

“目标”应该是选择器所属的对象,而不是 nil。