1

iOS5以降のiOSバージョンをサポートするアプリを作成しています。UIAlertView を使用しており、ユーザーがホーム ボタンを押したときに表示されている場合は、ユーザーがアプリに戻る前に閉じたいと思います (つまり、マルチタスクを使用してアプリを再度開いたときに消えてしまいます)。アプリ デリゲートのすべてのメソッドは、再度開いたときにまだ表示されている場合でも、非表示 (isVisible=NO) として表示されます。これを行う方法はありますか?

ありがとう。

4

2 に答える 2

7

または、UIAlertViewからクラスを継承し、UIApplicationWillResignActiveNotificationのNSNotificationオブザーバーを追加し、通知が発生したときにalertviewメソッドを呼び出します。dismissWithClickedButtonIndex:

例:.hファイル

#import <UIKit/UIKit.h>

@interface ADAlertView : UIAlertView

@end

.mファイル

#import "ADAlertView.h"

@implementation ADAlertView

- (void) dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (id) initWithTitle:(NSString *)title
             message:(NSString *)message
            delegate:(id)delegate
   cancelButtonTitle:(NSString *)cancelButtonTitle
   otherButtonTitles:(NSString *)otherButtonTitles, ... {
    self = [super initWithTitle:title
                        message:message
                       delegate:delegate
              cancelButtonTitle:cancelButtonTitle
              otherButtonTitles:otherButtonTitles, nil];

    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self
             selector:@selector(dismiss:)
                 name:UIApplicationDidEnterBackgroundNotification
               object:nil];
    }

    return self;
}

- (void) dismiss:(NSNotification *)notication {
    [self dismissWithClickedButtonIndex:[self cancelButtonIndex] animated:YES];
}

@end

UIAlertViewから継承された独自のクラスを使用すると、alertviewなどへのリンクを保存する必要はありません。UIAlertViewをADAlertView(または他のクラス名)に置き換える必要があるのは1つだけです。このコード例を自由に使用してください(ARCを使用していない場合は、[super dealloc]後にdeallocメソッドに追加する必要があります[[NSNotificatioCenter defaultCenter] removeObserver:self]

于 2013-02-21T17:50:53.470 に答える
4

UIAlertViewアプリ デリゲートに表示される への参照を保持します。アラートを表示するときは、参照を設定します。アラートが解除さnilれると、参照が無効になります。

アプリのデリゲートapplicationWillResignActive:またはメソッドで、アラート ビューへの参照でメソッドapplicationDidEnterBackground:を呼び出します。dismissWithClickedButtonIndex:animated:これにより、「ホーム」ボタンを押したときにそれを閉じることができます。

は電話などで呼び出されることに注意してくださいapplicationWillResignActive:。そのため、そのような場合にアラートを無視するか、電話でアラートを維持するかを決定する必要があります.

于 2013-02-21T17:39:36.600 に答える