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

发布于 2024-11-19 20:16:00 字数 829 浏览 3 评论 0原文

这是我的 UIBarButton

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

这是它应该启动的代码:

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

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

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

我调用选择器的方式是否做错了什么?

谢谢!

Here is my 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.

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

Thanks!

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

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

发布评论

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

评论(3

只想待在家 2024-11-26 20:16:01

您的按钮声明缺少一些内容,即 target 参数。试试这个:

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

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

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

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

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.

The target parameter is the instance that should handle the event.

故笙诉离歌 2024-11-26 20:16:01

对于那些仍然遇到此问题的人,这是我找到的另一个解决方案:
不要这样做:

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

尝试这样的操作:

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

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

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

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 UIBarButtonItem in the toolbar in Storyboard. (Or you could just programmatically create your own UIBarButtonItem before this set of code and add it to the items array of the UIToolbar.)

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?

青朷 2024-11-26 20:16:01

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

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

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