私はこのような通知を操作で投稿します:
DownloadStatus * status = [[DownloadStatus alloc] init];
[status setMessage: @"Download started"];
[status setStarted];
[status setCompleteSize: [filesize intValue]];
[userInfo setValue:status forKey:@"state"];
[[NSNotificationCenter defaultCenter]
postNotificationName:[targetURL absoluteString]
object:nil userInfo:userInfo];
[status release];
DownloadStatusは、現在ダウンロードされているダウンロードに関する情報を含むオブジェクトです。userInfoは、init部分で初期化され、操作の全期間にわたって保持されるオブジェクトのプロパティです。それは次のように作成されます:
NSDictionary * userInfo = [NSDictionary dictionaryWithObject:targetURL
forKey:@"state"];
「targetURL」はNSStringです。これは、すべてが正常に機能していることを確認するためだけに使用します。イベントを受け取ったとき-私は次のように登録しました:
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(downloadStatusUpdate:)
name:videoUrl
object:nil];
ここで、「videoUrl」はダウンロード中のURLを含む文字列であるため、ダウンロードされるのを待っているURLに関する通知を受け取ります。
セレクターは次のように実装されます。
- (void) downloadStatusUpdate:(NSNotification*) note {
NSDictionary * ui = note.userInfo; // Tried also [note userInfo]
if ( ui == nil ) {
DLog(@"Received an update message without userInfo!");
return;
}
DownloadStatus * state = [[ui allValues] objectAtIndex:0];
if ( state == nil ) {
DLog(@"Received notification without state!");
return;
}
DLog(@"Status message: %@", state.message);
[state release], state = nil;
[ui release], ui = nil; }
ただし、このセレクターは常にnullのuserInfoを受け取ります。私は何が間違っているのですか?
MrWHO