最好是创建一个协议:
@protocol MONStuffParserRecipientProtocol
@required
- (void)parsedStuffIsReady:(NSDictionary *)parsedStuff;
@end
并申报视图控制器:
@class MONStuffDownloadAndParserOperation;
@interface MONViewController : UIViewController <MONStuffParserRecipientProtocol>
{
MONStuffDownloadAndParserOperation * operation; // add property
}
...
- (void)parsedStuffIsReady:(NSDictionary *)parsedStuff; // implement protocol
@end
,并添加一些后端:到视图控制器
- (void)displayDataAtURL:(NSURL *)url
{
MONStuffDownloadAndParserOperation * op = self.operation;
if (op) {
[op cancel];
}
[self putUpLoadingIndicator];
MONStuffDownloadAndParserOperation * next = [[MONStuffDownloadAndParserOperation alloc] initWithURL:url recipient:viewController];
self.operation = next;
[next release], next = 0;
}
,并有该操作持有视图控制器:
@interface MONStuffDownloadAndParserOperation : NSOperation
{
NSObject<MONStuffParserRecipientProtocol>* recipient; // << retained
}
- (id)initWithURL:(NSURL *)url Recipient:(NSObject<MONStuffParserRecipientProtocol>*)recipient;
@end
,并有操作的消息时,数据被下载并解析收件人:
// you may want to message from the main thread, if there are ui updates
[recipient parsedStuffIsReady:parsedStuff];
存在实现一些事情 - 它只是一个形式。它更安全,涉及直接消息传递,参考计数,取消等。
请提供一些代码。 – 2011-04-08 08:23:08