HTTP データを送信し、エラーが発生した場合に UIAlertView を表示するメソッドがあります。複数の HTTP 投稿がある場合、エラーごとに複数の UIAlertView が表示されます。
他のUIAlertViewが表示されていない場合にのみ、UIAlertViewを表示したい。どうすればこれを判断できますか?
HTTP データを送信し、エラーが発生した場合に UIAlertView を表示するメソッドがあります。複数の HTTP 投稿がある場合、エラーごとに複数の UIAlertView が表示されます。
他のUIAlertViewが表示されていない場合にのみ、UIAlertViewを表示したい。どうすればこれを判断できますか?
UIAlertView クラスによって管理されている可視プロパティをチェックしてみませんか?
if (_alert) //alert is a retained property
{
self.alert = [[[UIAlertView alloc] initWithTitle:@"Your Title"
message:@"Your message"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK"] autorelease];
}
if (!_alert.visible)
{
[_alert show];
}
呼び出すオブジェクトで、UIAlertViewのshowメソッドを呼び出す前にivarを設定します。
...
if (!self.alertShowing) {
theAlert = [[UIAlertView alloc] initWithTitle:title message:details delegate:self cancelButtonTitle:nil otherButtonTitles:@"Okay", nil];
self.alertShowing = YES;
[theAlert show];
}
...
次に、アラートのデリゲートメソッドで、フラグivarをnoに設定します。
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
...
self.alertShowing = NO;
}
アラートを順番に表示したい場合は、通知を投稿して各メッセージをキューに追加し、アラートが閉じられた後にのみメッセージをキューから削除します。
他のアラート ビューを制御できる場合はvisible
、それぞれのプロパティを確認してください。
iOS 6 以前では、アラートが表示されると、_UIAlertOverlayWindow に移動されます。したがって、非常に壊れやすい方法は、すべてのウィンドウを反復処理して、UIAlertView サブビューがあるかどうかを確認することです。
for (UIWindow* window in [UIApplication sharedApplication].windows) {
NSArray* subviews = window.subviews;
if ([subviews count] > 0)
if ([[subviews objectAtIndex:0] isKindOfClass:[UIAlertView class]])
return YES;
}
return NO;
これは、内部のビュー階層に依存するため文書化されていませんが、Apple はこれについて文句を言うことはできません。より信頼性が高く、さらに文書化されていない方法は、 is nilかどうか[_UIAlertManager visibleAlert]
を確認することです。
これらのメソッドは、SpringBoard からの UIAlertView が表示されているかどうかを確認できません。
- (BOOL)checkAlertExist {
for (UIWindow* window in [UIApplication sharedApplication].windows) {
NSArray* subviews = window.subviews;
if ([subviews count] > 0) {
for (id cc in subviews) {
if ([cc isKindOfClass:[UIAlertView class]]) {
return YES;
}
}
}
}
return NO;
}
アプリ全体で機能し、ビュー スタックのウォークを伴わない別のオプションは、 にサブクラス化UIAlertView
しMyUIAlertView
、静的 (クラス) 変数を追加し、セレクターBOOL alertIsShowing
をオーバーライドすることです。-(void)show
オーバーライドさshow
れたセレクターで、alertIsShowing
変数を確認します。その場合はYES
、しばらくしてから再試行してください ( を使用dispatch_after
または設定しますNSTimer
)。の場合はNO
、呼び出し[super show]
て に割り当てYES
ますalertIsShowing
。アラート ビューが非表示になっている場合は、alertIsShowing
元に戻しNO
ます (デリゲートの処理には注意が必要です)。
UIAlertView
最後に、すべてのインスタンスをに置き換えますMyUIAlertView
。
私はそれがうまくいくと思います:
-(BOOL) doesAlertViewExist {
if ([[UIApplication sharedApplication].keyWindow isMemberOfClass:[UIWindow class]])
{
return NO;//AlertView does not exist on current window
}
return YES;//AlertView exist on current window
}
迅速:
func showAlert(withTitle title: String, message: String, viewController: UIViewController) {
if viewController.presentedViewController == nil { // Prevent multiple alerts at the same time
let localizedTitle = NSLocalizedString(title, comment: "")
let localizedMessage = NSLocalizedString(message, comment: "")
let alert = UIAlertController(title: localizedTitle, message: localizedMessage, preferredStyle: .Alert)
let action = UIAlertAction(title: "OK", style: .Default, handler: nil)
alert.addAction(action)
viewController.presentViewController(alert, animated: true, completion: nil)
}
}
+ (BOOL)checkAlertExist {
for (UIWindow* window in [UIApplication sharedApplication].windows) {
if ([window.rootViewController.presentedViewController isKindOfClass:[UIAlertController class]]) {
return YES;
}
}
return NO;
}