1

ユーザーから確認を受信せずに AlertView が一定時間画面に表示されている場合に、AlertView がタイムアウトする可能性があるかどうかを知りたいです。AlertView オブジェクトを NSTimer オブジェクトにリンクする方法はありますか?

私の基本的な AlertView コードは次のとおりです。

- (IBAction)showMessage:(id)sender {
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Hello World!"
                                                      message:@"This is your first UIAlertview message."
                                                     delegate:nil
                                            cancelButtonTitle:@"OK"
                                            otherButtonTitles:nil];
    [message show];
}
4

5 に答える 5

4

これが私のアプリの1つに実装した方法です

@interface 内でオブジェクトを宣言して、それらを追跡し、必要に応じて追加できるようにします

@property (nonatomic, strong) UIAlertView *myAlert;
@property (nonatomic, weak) NSTimer *myTimer;

アラートを起動する必要があるコードで、次を追加します

self.myAlert = [[UIAlertView alloc]initWithTitle:@"TEST" message:@"TEST" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
self.myTimer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(cancelAlert) userInfo:nil repeats:NO];
[self.myAlert show];

コードのどこかに次の関数を追加して、アラートを無視し、NSTimer を無効にします。

- (void)cancelAlert {
[self.myAlert dismissWithClickedButtonIndex:-1 animated:YES];
}

ボタンがタッチされた場合は、タイマーを無効にすることも忘れないでください。

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
[self.myTimer invalidate];
// Process pressed button
}

要件に合わせて微調整が必​​要になる場合があります。

于 2012-12-08T04:41:26.320 に答える
1

カテゴリを作成し、UIAlertViewリッスンするオブザーバーを追加し、トリガーされた場合はそれ自体を削除できます。

@implementation UIAlertView (Cancellable)

+ (instancetype)cancellableAlertViewWithTitle:(NSString *)title
                                      message:(NSString *)message
                                     delegate:(id)delegate
                            cancelButtonTitle:(NSString *)cancelButtonTitle
                            otherButtonTitles:(NSString *)otherButtonTitles, ...
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
                                                        message:message
                                                       delegate:delegate
                                              cancelButtonTitle:cancelButtonTitle
                                              otherButtonTitles:nil];
    if (otherButtonTitles != nil)
    {
        va_list args;
        va_start(args, otherButtonTitles);
        for (NSString *buttonTitle = otherButtonTitles; buttonTitle != nil; buttonTitle = va_arg(args, NSString*))
        {
            [alertView addButtonWithTitle:buttonTitle];
        }
        va_end(args);
    }

    [[NSNotificationCenter defaultCenter] addObserver:alertView selector:@selector(removeAlertView:) name:@"AlertsShouldBeCancelledNotification" object:nil];

    return alertView;
}

- (void)removeAlertView:(NSNotification *)notification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self dismissWithClickedButtonIndex:-1 animated:YES];
}

@end

次にNSTimer、メイン クラスで を作成し、セレクターが呼び出されたときに通知をトリガーすることができます。

于 2015-04-24T16:35:05.470 に答える
1

はい。使用するdismissWithClickedButtonIndex:animated:

たとえば、次のような dispatch_after ブロックの場合:

int64_t delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    [message dismissWithClickedButtonIndex:message.cancelButtonIndex animated:YES];
});

NSTimer を使用する場合UIAlertViewは、インスタンス変数に保存して、タイマー メソッド内からアクセスできるようにします。

于 2012-12-07T22:03:13.007 に答える
0

ユーザーが時間内にクリックした場合は、NSTimer を使用して呼び出しdismissWithClickedButtonIndex:animated:て無効にします。dispatch_after を使用すると、ユーザーが既に破棄している場合、リリースされたインスタンスにメッセージが送信される危険があります。

于 2012-12-07T22:09:16.963 に答える