我有一个 iOS 应用程序,它在 UITableView 中显示联系人姓名列表,并在点击单元格时使用 ABPersonViewController
显示联系人。该项目使用 ARC。如果我关联一个 ABPersonViewControllerDelegate
对象,应用程序会在显示人员 View 后立即在 AddressBookUI 框架中命中 EXC_BAD_ACCESS:
(lldb) bt
* thread #1: tid = 0x2403, 0x39ee15b0 libobjc.A.dylib`objc_msgSend + 16, stop reason = EXC_BAD_ACCESS (code=1, address=0x8)
frame #0: 0x39ee15b0 libobjc.A.dylib`objc_msgSend + 16
frame #1: 0x319eb880 AddressBookUI`-[ABPersonViewControllerHelper notifyScrollViewDidLoad] + 64
这是 UITableViewDelegate
方法(下面链接的完整项目)显示了 ABPersonViewController
和我的 ABPersonViewControllerDelegate
(实现为 BEVPVDelegate
) 被创建、关联和呈现:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ABPersonViewController *pvc = [[ABPersonViewController alloc] init];
BEVPVDelegate *pvDelegate = [[BEVPVDelegate alloc] init];
pvc.displayedPerson = (__bridge ABRecordRef)[self.contents objectAtIndex:[indexPath row]];
pvc.personViewDelegate = pvDelegate; // comment out this line to prevent the crash
[self.navigationController pushViewController:pvc animated:YES];
}
只需注释掉在 ABPersonViewController
实例上设置 personViewDelegate 属性的行即可消除异常,但我需要这个委托(delegate)对象。
这是 BEVPVDelegate.m 的全部内容:
//
// BEVPVDelegate.m
// ABCrash
//
#import "BEVPVDelegate.h"
@implementation BEVPVDelegate
- (BOOL)personViewController:(ABPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
NSLog(@"called");
return YES;
}
@end
这是一个演示问题的基本项目:ABCrash.zip on Dropbox
我的代码有问题吗?是否有允许我使用 ABPersonViewControllerDelegate
的解决方法?
编辑:我刚刚在 Instruments 中运行项目,我的 personViewDelegate 正在发布。如果我在 UIViewController 中为 personViewDelegate 创建一个保留属性,应用程序不会崩溃。
最佳答案
ABPersonViewController中的personViewDelegate属性是怎么定义的?我认为你让它变弱了。只需将其更改为强类型即可。 delegate 对象会在创建后立即释放,因为 weak 属性不会持有它。希望对您有所帮助。
关于ios - 使用 ABPersonViewControllerDelegate 时的 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16621355/