存储 NSTimer 的时间以供下次查看(帮助!)

发布于 2024-10-02 04:24:38 字数 1711 浏览 0 评论 0原文

我变得绝望了我的第二个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云仙小弟 2024-10-09 04:24:38

首先,请将您的第三个控制器代码移至问题中。

您没有保留 myticker。我很惊讶它竟然有效。

在文本字段中存储浮点数据是一个坏主意。最好使用 float 属性:

@interface MyController : UIViewController
{
  float currentTime;
  ...
}

@property (nonatomic, assign) float currentTime;
...

@end

@implementation MyController

@syntesize currentTime;

-(IBAction)start {
    self.myticker = [NSTimer scheduledTimerWithTimeInterval:0.01 
                                                target:self 
                                              selector:@selector(showActivity) 
                                              userInfo:nil 
                                               repeats:YES];
}

-(void)showActivity {
  currentTime += 0.01;
  time.text = [NSString stringWithFormat:@"%.2f", currentTime];        
}
...

@end

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:

@interface MyController : UIViewController
{
  float currentTime;
  ...
}

@property (nonatomic, assign) float currentTime;
...

@end

@implementation MyController

@syntesize currentTime;

-(IBAction)start {
    self.myticker = [NSTimer scheduledTimerWithTimeInterval:0.01 
                                                target:self 
                                              selector:@selector(showActivity) 
                                              userInfo:nil 
                                               repeats:YES];
}

-(void)showActivity {
  currentTime += 0.01;
  time.text = [NSString stringWithFormat:@"%.2f", currentTime];        
}
...

@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文