以下代码在 iOS7 中可以正常工作,但在 iOS8 中不能正常工作(变量 recordID
设置正确):
CFErrorRef error = nil;
const ABAddressBookRef addressBook = (ABAddressBookCreateWithOptions (NULL, &error));
ABRecordRef contactRef = ABAddressBookGetPersonWithRecordID (addressBook, recordID);
ABPersonViewController *personViewController = [[ABPersonViewController alloc] init];
personViewController.addressBook = addressBook;
personViewController.displayedPerson = contactRef;
CFRelease(addressBook);
NSArray *displayedProperties = @[@(kABPersonFirstNameProperty),
@(kABPersonLastNameProperty),
@(kABPersonMiddleNameProperty),
@(kABPersonPrefixProperty),
@(kABPersonSuffixProperty),
@(kABPersonOrganizationProperty),
@(kABPersonJobTitleProperty),
@(kABPersonDepartmentProperty),
@(kABPersonEmailProperty),
@(kABPersonBirthdayProperty),
@(kABPersonKindProperty),
@(kABPersonAddressProperty),
@(kABPersonPhoneProperty),
@(kABPersonInstantMessageProperty),
@(kABPersonURLProperty),
@(kABPersonSocialProfileProperty),
@(kABPersonNoteProperty)];
personViewController.displayedProperties = displayedProperties;
personViewController.navigationItem.title = NSLocalizedString(@"CONTACT_DETAILS", nil);
personViewController.allowsActions = YES;
personViewController.allowsEditing = YES; // if NO, no back button is shown
personViewController.personViewDelegate = self;
personViewController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"ADDRESSES",nil) style:UIBarButtonItemStylePlain target:self action:@selector(personviewDoneButtonPressed:)];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:personViewController];
[self presentViewController:navigationController animated:YES completion:nil];
iOS8 中的错误:
- 当
allowsEditing
设置为YES
时,显示联系人,但 只显示名称。导航栏左侧显示后退按钮(名为“地址”),右侧显示编辑按钮。当。。。的时候 按下编辑按钮,显示所有字段的联系人 除名称外为空,编辑按钮显示为完成 按钮。如果在没有任何编辑的情况下按下此完成按钮, 显示有关联系人的所有信息。 - 当
allowsEditing
设置为NO
时,不显示后退按钮,因此 屏幕不能再离开。
有人有解决方法吗?
更新:
我现在意识到,虽然总是在我的设备上,问题 1 只是有时发生在模拟器上>。
最佳答案
montuno(再次感谢)评论了我的第一个答案,从苹果技术支持那里得到了如何解决第一个问题的提示,我现在也明白了第二个问题:
1)只显示部分联系人:
Apple 声称以下代码是错误的:
ABRecordRef contactRef = ABAddressBookGetPersonWithRecordID (addressBook, recordID);
ABPersonViewController *personViewController = [[ABPersonViewController alloc] init];
personViewController.addressBook = addressBook;
personViewController.displayedPerson = contactRef;
CFRelease(addressBook);
此处 addressBook
在分配给 ABPersonViewController
后被错误地释放了。
在我的原始代码中,我没有 CFRelease
语句。但随后静态分析器警告称“地址簿中存储的对象可能发生泄漏”。在我插入 release 语句后,构建在没有警告的情况下成功,所以我假设 personViewController
保留了 addressBook
,显然不是这种情况,至少在 iOS8 中不是这样。我现在再次删除了发布声明,收到了警告,但一切正常。
2) 缺少后退按钮:
ABPersonViewController
的文档说“重要人物 View Controller 必须与导航 Controller 一起使用才能正常运行。”
所以我做了以下事情:
我设置了一个 ABPersonViewController
,为其 navigationItem.backBarButtonItem
分配了一个“Done”按钮,并初始化了一个新的 UINavigationController
使用 personViewController
,使用 initWithRootViewController:
。然后我以模态方式展示了新的 navigationController
。
显示带有后退按钮的 personViewController
,当按下时,呈现 View Controller 关闭模态呈现的导航 Controller 。
这在 iOS < 8 上运行良好,但在 iOS8 中,当 allowsEditing
设置为 NO
时,后退按钮不显示。
但这显然不是 Apple 希望显示 personViewController
的方式:
我认为 Apple 假设 navigationController
已经存在,并呈现当前 View ,因此新的 personViewController
只是被插入 navigationController
的堆栈>。在这种情况下,personViewController
不是 navigationController
的 rootViewController
,navigationController
会自动显示一个后退按钮(rootViewController
没有这样做)。如果以这种方式实现,一切也适用于 iOS8。
关于iOS8 漏洞 : ABPersonViewController does not display properties & back button,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26117245/