0

ボタンを押すと通知がポップアップするようにするためのコードがあります。10秒経過したら出てくるようにしたいです。

私はこれを行う必要があることを知っています:

(NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds invocation:
(NSInvocation *)invocation repeats:(BOOL)repeats

私の行動は:

{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView"
                                                message:[NSString stringWithFormat:@"%d", hi]
                                                   delegate:hi
                                          cancelButtonTitle:@"Ok"
                                          otherButtonTitles: nil];
    [alert show];


}
4

3 に答える 3

1

あなたはただ行うことができます:

[self performSelector:@selector(actionMethodName) withObject:nil afterDelay:10];

actionMethodName真のメソッド名に置き換えた場合。

タイマー ルートを使用する場合 (これも完全に有効です) scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:、呼び出しを作成するよりも簡単に使用できます。これを行う場合は、タイマーへの参照を保持し、完了したら無効にします。


更新された質問の詳細に基づいて、タイマーを使用して無効にする方法がより好ましい方法になりつつあります。実行セレクターをキャンセルすることは可能ですが、タイマールートほど明確ではありません。いずれにしても、Perform Selector ルートのコードを示します。

- (void)startEverything
{
    [self performSelector:@selector(showAlert) withObject:nil afterDelay:10];
}

- (void)handleButtonTap:(id)sender
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(showAlert) object:nil];
}

- (void)showAlert
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView"
                                                message:[NSString stringWithFormat:@"%d", hi]
                                                   delegate:nil
                                          cancelButtonTitle:@"Ok"
                                          otherButtonTitles:nil];
    [alert show];
}

タイマーを使用しても同じメソッド構造になりますが@property、タイマーを格納するために が必要になり、代わりに がcancelPreviousPerformRequestsWithTarget...必要になり[self.timer invalidate]; self.timer = nil;ます。

于 2013-07-15T13:42:52.250 に答える