Webサービス接続を処理するオブジェクトから、ネットワークに障害が発生したときに、Webサービスオブジェクトを使用するView Controllerに警告を渡します。UIAlertView didmissWithClickedButtonIndexが警告を閉じない
WebServiceObject:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[NSString stringWithFormat:@"Connection failed! You must be connected to a Wifi source to download data. Please reconnect to a Wifi source and try again later."] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
NSDictionary *alertDict = [NSDictionary dictionaryWithObjectsAndKeys:alert, @"AlertView", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:DisplayAlertNotification object:self userInfo:alertDict];
のViewController:
- (void)displayAlert:(NSNotification *)notification {
NSDictionary *dict = [notification userInfo];
if ([[dict objectForKey:@"AlertView"] isKindOfClass:[UIAlertView class]]) {
UIAlertView *alert = [dict objectForKey:@"AlertView"];
NSNumber *theTag = [dict objectForKey:@"AlertTag"];
NSLog(@"%i", [theTag integerValue]);
alert.tag = [[dict objectForKey:@"AlertTag"] integerValue];
[alert show];
}
}
- (void)removeAlert:(NSNotification *)notification {
NSDictionary *dict = [notification userInfo];
if ([[dict objectForKey:@"AlertTag"] isKindOfClass:[NSNumber class]]) {
NSNumber *theTag = [dict objectForKey:@"AlertTag"];
UIAlertView *alert = (UIAlertView *)[self.view viewWithTag:[theTag integerValue]];
// Not sure why but my alert is nil at this point
[alert dismissWithClickedButtonIndex:0 animated:YES];
}
}
私はまた、プログラムのアラートを削除するのと同じ方法でremoveAlertメソッドを使用します。これは、ネットワークに障害が発生してもユーザーが[OK]をクリックしていない状態でネットワークが復旧した場合、Network Failedアラートを消し、Network Resumedアラートを表示することです。アラートを終了してNetwork Resumedを表示した後で、[Network Resumed]で[Ok]をクリックすると、元のNetwork Failedが1回だけ元に戻ります。 Network Failedが表示されている間にユーザーがOkをクリックした場合、それは決して元に戻りません。
アラートをこのように正しく削除しますか?ありがとう。
編集:WebServiceObjectに参照を保存し、それを却下するだけで動作させることができます。
もしないあなたは皆無一部で正しいです。警報がまだ表示されていたので、後でそれを追加し、間違った場所に追加しました。それがなくても、アラートは消えません。 – Crystal
問題はあなたが正しい "警告"を得なかったかもしれないので、 "viewWithTag"はnilを返すかもしれません。ただそれを確認してください – adali
removeAlertの[dict objectForKey:@ "AlertView"]で "alert" ?おそらくあなたは、メンバー変数として "警告"を設定する必要があります – adali