当您切换到其他页面并完成其他任务时,我试图让计时器在另一个页面上运行,本质上是在记录完成任务所需的时间。每当我切换到另一个页面时,它都会将计时器重置为开始时的状态,并且对我试图保持打开的其他页面上的一些开关执行相同的操作。有任何想法吗?
故事板截图:
到目前为止的代码:
//
// ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (IBAction)start{
ticker = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self ``selector:@selector(showActivity) userInfo:nil repeats:YES];
}
- (IBAction)reset{
[ticker invalidate];
time.text = @" 0:00";
}
- (void)showActivity{
int currentTime = [time.text intValue];
int newTime = currentTime + 1;
time.text = [NSString stringWithFormat:@"%d", newTime];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
// ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController{
IBOutlet UILabel *time;
NSTimer *ticker;
}
- (IBAction)start;
- (IBAction)reset;
- (void)showActivity;
@end