0

我读到了Change the delegate of MGTwitterEngine并没有真正明白。我希望有人能再次解释一下。

根据我所知道的,我为 MGTwitterEngine 创建了一个包装器,并在包装​​器中设置了委托。所以为了更容易,我尝试为接口创建一个 NSArray 实例,我会在需要时传递它。

这是收到的状态的代码:

- (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)connectionIdentifier
{
    //NSLog(@"Got statuses for %@:\r%@", connectionIdentifier, statuses);

    [statusIds setObject:statuses forKey:connectionIdentifier];
}

因此,我希望项目中的任何对象都可以访问 sharedTwitterEngine,只要我先请求信息,然后释放 statusContainer,使用新结果并将其传递给我的工作对象以供以后使用。

我不确定这是否是正确的方法,还是我错过了更简单的方法?

4

1 回答 1

1

您链接的SO帖子提出的解决方案可以通过这种方式实现:

1) 为 MGTwitterEngine 创建一个包装器;此包装器将公开您需要的任何 MGTwitterEngine 选择器,并将向每个选择器添加一个参数,该参数标识正在调用它的视图控制器;

2)您对 MGTwitterEngine 的包装器将充当所有发送请求的唯一代表;

3) 对于包装器从视图控制器接收到的每个请求,包装器会将视图控制器地址存储在与 twitter id 关联的 NSMutableDictionary 中;

4)当响应返回时,委托(与包装器相同的对象)将找出最初发送请求的视图控制器(通过在字典中搜索响应附带的 twitter id),并转发回应它。

我希望这有帮助....

编辑:

这就是你可以做到的(我只包括 1 个 API 调用和相关代码):

@interface TwitterClientViewController : UIViewController <MGTwitterEngineDelegate> {
}
@end

@implementation TwitterClientViewController;

- (void)requestListOfUsers:(NSString*)username {
    [twitterEngineSingleton getListsForUser:username requestDelegate:self];
}

- (void)requestSucceeded:(NSString*)connectionIdentifier {
    NSLog(@"Hello");
}
@end

@interface AdvancedTwitterEngine : NSObject <MGTwitterEngineDelegate> {
    MGTwitterEngine* _engine;
    NSMutableDictionary* _callerIds;
}
-(NSString*)getListsForUser:(NSString*)username requestDelegate:(id<MGTwitterEngineDelegate>)delegate;
@end

@implementation AdvancedTwitterEngine;

-(void)init {
    if (self = [super init]) {
        _engine = [[MGTTwitterEngine alloc] initWithDelegate:self];
        _callerIds = <init>
    }
    return self;
}

-(NSString*)getListsForUser:(NSString*)username requestDelegate:(id<MGTwitterEngineDelegate>)delegate {
    NSString* twId = [_engine getListsForUser:username];
    [_callerIds setObject:controller forKey:twId];
    return twId;
}

//-- delegate methods

- (void)requestSucceeded:(NSString*)connectionIdentifier {
    id<MGTwitterEngineDelegate> dlg = [_callerIds objectForKey:connectionIdentifier];
    [dlg requestSucceeded:connectionIdentifier];
}

@end
于 2011-05-19T07:27:48.013 回答