42

HTTP データを送信し、エラーが発生した場合に UIAlertView を表示するメソッドがあります。複数の HTTP 投稿がある場合、エラーごとに複数の UIAlertView が表示されます。

他のUIAlertViewが表示されていない場合にのみ、UIAlertViewを表示したい。どうすればこれを判断できますか?

4

10 に答える 10

60

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];
}
于 2012-04-19T12:55:39.213 に答える
52

呼び出すオブジェクトで、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;
}

アラートを順番に表示したい場合は、通知を投稿して各メッセージをキューに追加し、アラートが閉じられた後にのみメッセージをキューから削除します。

于 2010-03-27T14:27:15.657 に答える
26

他のアラート ビューを制御できる場合は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 が表示されているかどうかを確認できません。

于 2010-03-27T14:49:06.437 に答える
9
- (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;
}
于 2013-03-01T06:28:14.433 に答える
4

アプリ全体で機能し、ビュー スタックのウォークを伴わない別のオプションは、 にサブクラス化UIAlertViewMyUIAlertView、静的 (クラス) 変数を追加し、セレクターBOOL alertIsShowingをオーバーライドすることです。-(void)show

オーバーライドさshowれたセレクターで、alertIsShowing変数を確認します。その場合はYES、しばらくしてから再試行してください ( を使用dispatch_afterまたは設定しますNSTimer)。の場合はNO、呼び出し[super show]て に割り当てYESますalertIsShowing。アラート ビューが非表示になっている場合は、alertIsShowing元に戻しNOます (デリゲートの処理には注意が必要です)。

UIAlertView最後に、すべてのインスタンスをに置き換えますMyUIAlertView

于 2013-10-03T09:24:35.003 に答える
3

私はそれがうまくいくと思います:

-(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
}
于 2016-08-25T06:09:32.693 に答える
3

迅速:

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)
    }
}
于 2016-07-12T12:35:12.720 に答える
2
+ (BOOL)checkAlertExist {

    for (UIWindow* window in [UIApplication sharedApplication].windows) {
        if ([window.rootViewController.presentedViewController isKindOfClass:[UIAlertController class]]) {
            return YES;
        }
    }
    return NO;
}
于 2016-03-23T16:14:57.243 に答える