本文介绍了在Iphone中创建一个StopWatch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个简单的秒表。我提到了这个网站()做这个应用程序。当我点击开始按钮时,我得到-596:-31:-23:-648并且秒表没有运行。
I am trying to create a simple stopwatch. I referred to this website ( http://www.apptite.be/tutorial_ios_stopwatch.php) to do this app. When I click the start button, I get -596:-31:-23:-648 and the stopwatch does not run.
代码如下:
ViewController.h
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController{
UILabel *lbl;
NSTimer *stopTimer;
NSDate *startDate;
}
@property (strong,nonatomic) IBOutlet UILabel *lbl;
-(IBAction)startPressed:(id)sender;
-(IBAction)stopPressed:(id)sender;
-(void)updateTimer;
@end
ViewController.m
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize lbl;
- (void)viewDidLoad
{
[super viewDidLoad];
lbl.text = @"00.00.00.000";
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)startPressed:(id)sender{
if (stopTimer == nil) {
stopTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
target:self
selector:@selector(updateTimer)
userInfo:nil
repeats:YES];
}
}
-(IBAction)stopPressed:(id)sender{
[stopTimer invalidate];
stopTimer = nil;
//[self updateTimer];
}
-(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;
}
@end
需要一些指导来解决错误。不确定错误在哪里。感谢任何帮助...
Need some guidance on solving the error. Not sure where the error is. Appreciate any help...
推荐答案
解决了错误:
ViewController.h
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController{
UILabel *lbl;
NSTimer *stopTimer;
NSDate *startDate;
BOOL running;
}
@property (strong,nonatomic) IBOutlet UILabel *lbl;
-(IBAction)startPressed:(id)sender;
-(IBAction)resetPressed:(id)sender;
-(void)updateTimer;
@end
ViewController.m
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize lbl;
- (void)viewDidLoad
{
[super viewDidLoad];
lbl.text = @"00.00.00.000";
running = FALSE;
startDate = [NSDate date];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(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;
}
@end
代码略有改动已经制作..但它现在正常工作......
Slight alterations to the code have been made.. But it is working correctly now...
这篇关于在Iphone中创建一个StopWatch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!