以编程方式创建UIBarButtonItem Not Launching Selector Action

Art*_*sev 8 xcode objective-c uinavigationcontroller uibarbuttonitem ios

这是我的UIBarButton:

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

这是它应该启动的代码:

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

    [self presentModalViewController:picker animated:YES];
    [picker release];
}
Run Code Online (Sandbox Code Playgroud)

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

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

谢谢!

age*_*ped 24

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

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

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

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