2013-03-08 73 views
0

我想使用NSTimer重複觸發一個方法。但該方法只觸發一次。 這裏是我的代碼NSTimer不要重複(iOS開發)

- (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薄荷只火一次,不repeat.Why?如何解決?

更新: 我很抱歉,定時器工作正常,問題在於軒轅方法。我現在沒有問題。

+0

你想在14秒後打電話給玄璇方法。對 ? – 2013-03-08 04:53:18

+2

應該沒問題,你在軒轅內做什麼? – borrrden 2013-03-08 04:53:50

+0

你想只打2遍或者每14秒鐘打完一遍? – 2013-03-08 04:57:43

回答

1

嘗試:

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

代替上面的代碼。

0
在.m文件.h文件中

NSTimer *timer; 

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

這將每14秒後打電話給你xuanZhuan mthod。

0

這樣的使用定時器:

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

這將每14秒後觸發的方法。