这种情况是我有多个视图想要调用地址簿。为了不在每个视图中重复委托的代码,我在 App Delegate 的标头和 .m 文件中找到了代码,但在两个相应 App 的底部使用了“@interface AddressBookDelegate”和“@implementation AddressBookDelegate”代表——
@interface AddressBookDelegate : UIViewController <ABPeoplePickerNavigationControllerDelegate> {
AddressBookDelegate *addressBookDelegate;
}
@property (nonatomic, retain) AddressBookDelegate *addressBookDelegate;
@end
和
@implementation AddressBookDelegate
@synthesize addressBookDelegate;
- (void)peoplePickerNavigationControllerDidCancel: (ABPeoplePickerNavigationController *)peoplePicker
{
[self dismissModalViewControllerAnimated:YES];
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
[super dismissModalViewControllerAnimated:YES];
...get stuff from the Address Book...
return NO;
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
return NO;
}
然后在我看来,我有以下代码:
addressBookDelegate = (AddressBookDelegate *) [[UIApplication sharedApplication] delegate];
ABPeoplePickerNavigationController *abPicker = [[ABPeoplePickerNavigationController alloc]init];
abPicker.peoplePickerDelegate = self.addressBookDelegate;
[self presentModalViewController:abPicker animated:YES];
[abPicker release];
通讯簿在所有视图中都显示良好。但是当我采取任何会调用委托的用户操作时,比如地址簿的取消按钮,我崩溃了——
-[MyprogAppDelegate peoplePickerNavigationControllerDidCancel:]:发送到实例的无法识别的选择器
它编译干净,没有警告。
当 peoplePickerDelegate 与视图本身不在同一个文件中时,如何连接它以连接到 Address Delegate 代码?谢谢。
添加注意:当我使用调试器并停止在线时
abPicker.peoplePickerDelegate = addressBookDelegate;
在视图代码中,我看到 addressBookDelegate 的地址被声明为 MyprogAppDelegate 的地址,而不是我预期的 AddressBookDelegate 的地址。这让我觉得地址簿委托代码的置换在 App Delegate 文件中是关闭的。
如果 AddressBookDelegate Cancel Delegate 代码在 AddressBookDelegate 中输入了 1000 个字节,那么我的应用实际上是在将 1000 个字节的代码“输入”到 MyprogAppDelegate 中,因此会崩溃。所以不知何故,我没有正确设置 AddressBookDelegate 的寻址。反正这是我的看法...