最好是創建一個協議:
@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