2

しばらくしてから (アプリで何かを実行してから 5 分後など)、UIAlertView を表示しようとしています。アプリが閉じているかバックグラウンドであるかは、すでにユーザーに通知しています。しかし、アプリの実行中に UIAlertView を表示したいのです。

次のようにdispatch_asyncを試みましたが、アラートは永遠にポップしています:

[NSThread sleepForTimeInterval:minutes];
 dispatch_async(dispatch_get_main_queue(),
       ^{
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"title!" message:@"message!" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
        [alert show];
        [alert release];
       }
       );

また、スレッドは 30 ~ 60 分後に死ぬと読みました。60分以上経過したらアラートを表示できるようにしたいです。

4

2 に答える 2

12

を使用しないのはNSTimerなぜですか? この場合、なぜ GCD を使用する必要があるのでしょうか?

[NSTimer scheduledTimerWithTimeInterval:5*60 target:self selector:@selector(showAlert:) userInfo:nil repeats:NO];

次に、同じクラス内で、次のようになります。

- (void) showAlert:(NSTimer *) timer {
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"title!" 
                                                     message:@"message!" 
                                                    delegate:self               
                                           cancelButtonTitle:@"Cancel"
                                           otherButtonTitles:nil];
    [alert show];
    [alert release];
}

また、@ PeyloWが指摘したように、次のものも使用performSelector:withObject:afterDelay:できます。

UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"title!" 
                                                 message:@"message!" 
                                                delegate:self               
                                       cancelButtonTitle:@"Cancel"
                                       otherButtonTitles:nil];
[alert performSelector:@selector(show) withObject:nil afterDelay:5*60];
[alert release];

編集dispatch_afterGCD のAPIも使用できるようになりました。

double delayInSeconds = 5;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title!"
                                                        message:@"message"
                                                       delegate:self
                                              cancelButtonTitle:@"Cancel"
                                              otherButtonTitles:nil];
    [alertView show];
    [alertView release]; //Obviously you should not call this if you're using ARC
});
于 2011-01-04T19:41:49.477 に答える
0

これは、ローカル通知が作成された種類のものです。アプリがバックグラウンドで実行されているか、まったく実行されていない場合でも、UIAlertViewのような通知が将来表示されるように設定できます。

これがチュートリアルです。

于 2011-01-04T21:58:41.420 に答える