存储 NSTimer 的时间以供下次查看(帮助!)
我变得绝望了我的第二个 ViewController 中有一个 NSTimer。
-(IBAction)start {
myticker = [NSTimer scheduledTimerWithTimeInterval:0.01
target:self
selector:@selector(showActivity)
userInfo:nil
repeats:YES];
}
-(IBAction)stop {
[myticker invalidate];
}
-(void)showActivity {
float currentTime = [time.text floatValue];
newTime = currentTime + 0.01;
time.text = [NSString stringWithFormat:@"%.2f", newTime];
}
newTime
是一个实例变量(浮点数),time
是显示时间的标签。
在调用第三个 ViewController 之前,我存储了时间:
-(IBAction)right {
[self stop];
ThirdViewController *screen = [[ThirdViewController alloc] initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
screen.timelabel.text = [NSString stringWithFormat: @"%.2f", newTime];
[self presentModalViewController:screen animated:YES];
[screen release];
}
我停止了计时器并将时间赋予实例变量 timelabel (UILabel)。 在第三个 ViewController 中,我使用相同的方法创建了相同的 NSTimer。 但是当我打开第三个视图控制器时,计时器再次从 0 开始。
我的代码有什么问题吗?
我的第三个 ViewController:
-(IBAction)start {
myticker = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(showActivity) userInfo:nil repeats:YES];
}
-(IBAction)stop {
[myticker invalidate];
}
-(void)showActivity {
float currentTime = [timelabel.text floatValue];
float newTime = currentTime + 0.01;
timelabel.text = [NSString stringWithFormat:@"%.2f", newTime];
}
I'm becomming desperate. I've got a NSTimer in my second ViewController.
-(IBAction)start {
myticker = [NSTimer scheduledTimerWithTimeInterval:0.01
target:self
selector:@selector(showActivity)
userInfo:nil
repeats:YES];
}
-(IBAction)stop {
[myticker invalidate];
}
-(void)showActivity {
float currentTime = [time.text floatValue];
newTime = currentTime + 0.01;
time.text = [NSString stringWithFormat:@"%.2f", newTime];
}
newTime
ist an instance variable (float) and time
is the label where the time is shown.
Before calling the third ViewController, I stored the time this:
-(IBAction)right {
[self stop];
ThirdViewController *screen = [[ThirdViewController alloc] initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
screen.timelabel.text = [NSString stringWithFormat: @"%.2f", newTime];
[self presentModalViewController:screen animated:YES];
[screen release];
}
I stopped the timer and gave the time to the instance variable timelabel
(UILabel).
In the third ViewController I created the same NSTimer with the same methods.
But when I open the third View Controller the timer starts from 0 again.
What's wrong about my code?
My third ViewController:
-(IBAction)start {
myticker = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(showActivity) userInfo:nil repeats:YES];
}
-(IBAction)stop {
[myticker invalidate];
}
-(void)showActivity {
float currentTime = [timelabel.text floatValue];
float newTime = currentTime + 0.01;
timelabel.text = [NSString stringWithFormat:@"%.2f", newTime];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,请将您的第三个控制器代码移至问题中。
您没有保留
myticker
。我很惊讶它竟然有效。在文本字段中存储浮点数据是一个坏主意。最好使用 float 属性:
First of all, please move you third controller code to the question.
You are not retaining
myticker
. I'm surprised that it works at all.It is a bad idea to store float data in text field. It would be better to use float property for that: