嗨我想使用NSTimer创建一个倒计时,并使用标签查看此。问题在于标签出现在两个标签中,我不知道为什么。有任何想法吗?iOS NSTimer发射两次
继承人我的代码
- (void)viewDidLoad
{
NSLog(@"%@", self.chosenTime);
[self startGame];
[super viewDidLoad];
NSString *timeString = self.chosenTime;
self.timer = [timeString intValue];
self.countdown.text = timeString;
// Do any additional setup after loading the view.
}
- (void)startGame
{
[self introCountdown];
[self performSelector:@selector(goAnimation) withObject:nil afterDelay:3.0];
NSString *timeString = self.chosenTime;
self.timer = [timeString intValue];
[self performSelector:@selector(startCountdown) withObject:nil afterDelay:4.0];
}
- (void)startCountdown
{
//self.countdownTimer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(decrementTimer:) userInfo:nil repeats:YES];
self.countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.00 target:self selector:@selector(decrementTimer:) userInfo:nil repeats:YES];
}
- (void)decrementTimer:(NSTimer *)timer
{
if(self.timer > 0)
{
self.timer = self.timer - 1;
self.countdown.text = [NSString stringWithFormat:@"%d", self.timer];
}
else
{
[self.countdownTimer invalidate];
self.countdownTimer = nil;
}
}
你可以发布你调用startCountDown的代码吗? – giorashc
嗯..你可以尝试显式调用startCountDown([self startCountdown])并查看它是否工作? – giorashc
BTW [super viewDidLoad]应该首先在viewDidLoad()中执行 – giorashc