0

NSTimer を使用してメソッドを繰り返しトリガーしたいのですが、メソッドは一度しかトリガーされません。ここに私のコードがあります

- (void)viewDidLoad
{

[super viewDidLoad];   
NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:14.0];
NSTimer *timer = [[NSTimer alloc] initWithFireDate:fireDate
                                              interval:2
                                                target:self
                                              selector:@selector(xuanZhuan:)
                                              userInfo:nil
                                               repeats:YES];

NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:timer forMode:NSDefaultRunLoopMode];     
}


-(void)xuanZhuan:(NSTimer*)theTimer
{
    ...
}

XuanZhuan メントールは 1 回だけ発火し、繰り返しません。なぜですか?どのように修正しますか?

更新: 申し訳ありません。タイマーは正常に動作します。問題は xuanZhuan メソッドにあります。今は疑問がありません。

4

3 に答える 3

1

試す:

 [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(xuanZhuan:) userInfo:nil repeats:YES];

上記のコードの代わりに。

于 2013-03-08T05:01:38.027 に答える
0

.hファイル内

NSTimer *timer;

.mファイル内

timer =[NSTimer scheduledTimerWithTimeInterval:14.0 target:self selector:@selector(xuanZhuan) userInfo:nil repeats:YES];

これによりxuanZhuan、14秒ごとにmthodが呼び出されます。

于 2013-03-08T05:02:22.673 に答える
0

このようなタイマーを使用してください:

timer=[NSTimer scheduledTimerWithTimeInterval:14 target:self selector:@selector(xuanZhuan) userInfo:Nil repeats:YES];

これにより、14秒ごとにメソッドがトリガーされます。

于 2013-03-08T05:02:47.510 に答える