我正在尝试使用预定义方法
从addressBook
获取更新
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
ABAddressBookRegisterExternalChangeCallback(addressBook, addressBookChanged, self);
void addressBookChanged(ABAddressBookRef addressBook, CFDictionaryRef info, void *context)
{
NSLog(@"AddressBook Changed");
[self getContactsFromAddressBook];
}
我在ABAddressBookRegisterExternalChangeCallback(addressBook, addressBookChanged, self);
中调用application:didFinishLaunchingWithOptions
,然后我执行回调方法,如何在c方法中使用self?如果我不能使用我的对象,如何更新我的tableview?
答案 0 :(得分:5)
您不能在该函数中直接使用self
- 但是当您注册更改回调时,您将传递self作为上下文,因此它将作为addressBookChanged函数中的参数传递。
void addressBookChanged(ABAddressBookRef addressBook, CFDictionaryRef info, void *context)
{
NSLog(@"AddressBook Changed");
YourClass *yourInstance = (__bridge YourClass *)(context)
[yourInstance getContactsFromAddressBook];
}
更具体到您的课程 -
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
ABAddressBookRegisterExternalChangeCallback(addressBook, addressBookChanged, self.wkListVC);
return YES;
}
void addressBookChanged(ABAddressBookRef addressBook, CFDictionaryRef info, void *context)
{
NSLog(@"AddressBook Changed");
wbkABViewControllerTableViewController *myVC = (__bridge wbkABViewControllerTableViewController *)context;
[myVC getPersonOutOfAddressBook];
}
当您注册更改回调时,请确保self.wkListVC不是nil。