方法返回在委托中獲得的結果

[英]method return result obtained in delegate


I have the following method, which basically tries to get the user profile picture from facebook/twitter. The issue is that the result will be returned in a delegate, so therefore profilePicture results is obtained in the delegate called by these two methods. How do I deal with this?

我有以下方法,基本上試圖從Facebook / Twitter獲取用戶個人資料圖片。問題是結果將在委托中返回,因此profilePicture結果是在這兩種方法調用的委托中獲得的。我該如何處理?

- (NSString *) profilePicture
{
    NSString * result;
    if (facebook.isSessionValid){
        [facebook requestWithGraphPath:@"me/picture"
                             andParams:nil
                         andHttpMethod:@"GET"
                           andDelegate:self];
    } else if ([_twitter isAuthorized]){
        [_twitter getUserInformationFor:[_twitter username]];
    }
    return result;
}

2 个解决方案

#1


1  

You are setting the delegate to self. So you need to implement protocol methods that the facebook class specifies in this class.

您正在將委托設置為自己。所以你需要實現facebook類在這個類中指定的協議方法。

In Facebook API, the protocol is named FBRequestDelegate and the method you need to implement is request:didLoad:

在Facebook API中,協議名為FBRequestDelegate,您需要實現的方法是request:didLoad:

Example: taken from this page

示例:摘自此頁面

- (void)request:(FBRequest *)request didLoad:(id)result {
    if ([result isKindOfClass:[NSArray class]]) {
        result = [result objectAtIndex:0];
    }
    NSLog(@"Result of API call: %@", result);
}

#2


0  

BY calling "[facebook requestWithGraphPath:@"me/picture" andParams:nil andHttpMethod:@"GET" andDelegate:self];" you should be creating an object of FBRequest.

通過調用“[facebook requestWithGraphPath:@”me / picture“andParams:nil andHttpMethod:@”GET“andDelegate:self];”你應該創建一個FBRequest的對象。

You could customize the FBRequest, Add a new typedef

您可以自定義FBRequest,添加新的typedef

typedef enum
{
    ...
    currentactionProfilePictureAccess,
    ...
}currentaction;

@interface FBRequest:<superclass>
{
    .....
    currentaction currentactiontype;
}
.....
@property(nonatiomic,asign) currentaction currentactiontype;
....

@end

also synthesize it in implementation.

也在實施中合成它。

And while creating the request specify the type of request(currentactiontype) you are doing.

在創建請求時,請指定您正在執行的請求類型(currentactiontype)。

When the result comes in "- (void)request:(FBRequest *)request didLoad:(id)result Check type of request and process.

當結果進入“ - (void)請求:(FBRequest *)請求didLoad:(id)結果檢查請求和進程的類型。

if(request.currentactiontype==currentactionProfilePictureAccess)
{
    //call what ever you want.
}

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2011/09/04/3e9e6e310014c403c7e657d16dd56c49.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com