以编程方式创建UIBarButtonItem Not Launching Selector Action [英] Programmatically created UIBarButtonItem Not Launching Selector Action
问题描述
这是我的 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];
}
当我启动应用程序并点击+联系人 UIBarButton
,没有任何反应。没有错,纳达。我输入了一个断点,它永远不会到达选择器引用的方法。
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?
谢谢!
推荐答案
您的按钮声明缺少某些内容,即 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];
这假设 showPicker:
实际上是在将该按钮添加到导航项的同一类中。
This assumes that showPicker:
is in fact in the same class that's adding the button to the navigation item.
target
参数是应该处理的实例事件。
The target
parameter is the instance that should handle the event.
这篇关于以编程方式创建UIBarButtonItem Not Launching Selector Action的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!