以编程方式创建的 UIBarButtonItem 未启动选择器操作
这是我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的按钮声明缺少一些内容,即
target
参数。试试这个:这假设
showPicker:
实际上位于将按钮添加到导航项的同一类中。target
参数是应该处理事件的实例。The declaration of your button is missing something, namely the
target
parameter. Try this: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.对于那些仍然遇到此问题的人,这是我找到的另一个解决方案:
不要这样做:
尝试这样的操作:
*确保您已在 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:
Try something like this:
*Make sure you've added this
UIBarButtonItem
in the toolbar in Storyboard. (Or you could just programmatically create your ownUIBarButtonItem
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?
“目标”应该是选择器所属的对象,而不是 nil。
The "target" should be the object the selector belongs to, instead of nil.