我有一個方法發出請求並有一個代表響應者。該方法由KVO響應者調用。當我從viewDidLoad調用它時,代碼工作正常,但是當我從KVO方法發出請求時,它不會觸發委託方法。iOS代理問題與KVO
這裏是我的志願響應:
//observe app delegates userdata to check for when user data is available or data is changed
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
if ([keyPath isEqualToString:@"userData"])
{
//goto facebook logic
[self FBFriendSuggester];
}
}
並調用方法:
-(void)FBFriendSuggester
{
NSDictionary * fb_credentials = nil;
if([prefs objectForKey:@"facebookCredentials"])
{
fb_credentials = [prefs objectForKey:@"facebookCredentials"];
if ([fb_credentials objectForKey:@"expiry"] && [fb_credentials objectForKey:@"fbtoken"]) {
NSLog(@"found stored Facebook credentials.");
ApplicationDelegate.facebook.accessToken = [fb_credentials objectForKey:@"fbtoken"];
ApplicationDelegate.facebook.expirationDate = [fb_credentials objectForKey:@"expiry"];
}
}
if ([ApplicationDelegate.facebook isSessionValid]) {
printf("_valid facebook\n");
[ApplicationDelegate.facebook requestWithGraphPath:@"me/friends" andDelegate:self];
}else
printf("_invalid facebook\n");
}
和委託方法:
- (void)request:(FBRequest *)request didLoad:(id)result{
NSLog(@"result2:%@",result);
NSDictionary * rawObject = result;
NSArray * dataArray = [rawObject objectForKey:@"data"];
for (NSDictionary * f in dataArray) {
[friends addObject:[[KNSelectorItem alloc] initWithDisplayValue:[f objectForKey:@"name"]
selectValue:[f objectForKey:@"id"]
imageUrl:[NSString stringWithFormat:@"http://graph.facebook.com/%@/picture?type=square", [f objectForKey:@"id"]]]];
}
[friends sortUsingSelector:@selector(compareByDisplayValue:)];
[self pickerPopup:self];
}
委託方法不會被調用在這種情況下,但它確實工作正常時,我直接從控制器調用 - (void)FBFriendSuggester。我不知道該怎麼做,我試着設置一個通知來回應,希望它能觸發代表,但那也不起作用。