我的主要故事板有一个秒表。我可以启动手表,然后导航到另一个故事板。当我返回主秒表故事板时,它已停止并重置为零。请帮我弄清楚如何让秒表继续运行直到我按下STOP键。感谢
所有故事板都有相同的控制器
here is the .h
#import <UIKit/UIKit.h>
@interface MainViewController : UIViewController{
UILabel *lbl;
NSTimer *stopTimer;
NSDate *startDate;
BOOL running;
}
@property (weak, nonatomic) IBOutlet UIBarButtonItem *sidebarButton;
@property (strong,nonatomic) IBOutlet UILabel *lbl;
-(IBAction)startPressed:(id)sender;
-(IBAction)resetPressed:(id)sender;
-(void)updateTimer;
@end
here is the .m
#import "MainViewController.h"
#import "SWRevealViewController.h"
@interface MainViewController ()
@end
@implementation MainViewController
@synthesize lbl;
- (void)viewDidLoad
//stopwatch open
{
[super viewDidLoad];
lbl.text = @"00.00.00.000";
running = FALSE;
startDate = [NSDate date];
//stopwatch close
[super viewDidLoad];
self.title = @"therapyTools";
// Change button color
_sidebarButton.tintColor = [UIColor colorWithWhite:0.1f alpha:0.9f];
// Set the side bar button action. When it's tapped, it'll show up the sidebar.
_sidebarButton.target = self.revealViewController;
_sidebarButton.action = @selector(revealToggle:);
// Set the gesture
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//stopwatch open
-(IBAction)startPressed:(id)sender{
if(!running){
running = TRUE;
[sender setTitle:@"STOP" forState:UIControlStateNormal];
if (stopTimer == nil) {
stopTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
target:self
selector:@selector(updateTimer)
userInfo:nil
repeats:YES];
}
}else{
running = FALSE;
[sender setTitle:@"START" forState:UIControlStateNormal];
[stopTimer invalidate];
stopTimer = nil;
}
}
-(void)updateTimer{
NSDate *currentDate = [NSDate date];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm:ss.SSS"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
NSString *timeString=[dateFormatter stringFromDate:timerDate];
lbl.text = timeString;
}
-(IBAction)resetPressed:(id)sender{
[stopTimer invalidate];
stopTimer = nil;
startDate = [NSDate date];
lbl.text = @"00.00.00.000";
running = FALSE;
}
//stopwatch close
@end