在Iphone中创建秒表

在Iphone中创建秒表,iphone,ios,nstimer,Iphone,Ios,Nstimer,我正在尝试创建一个简单的秒表。我参考了这个网站()来做这个应用。当我点击开始按钮时,我得到-596:-31:-23:-648并且秒表不运行 代码如下所示: ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIViewController{ UILabel *lbl; NSTimer *stopTimer; NSDate *startDate; } @property

我正在尝试创建一个简单的秒表。我参考了这个网站()来做这个应用。当我点击开始按钮时,我得到-596:-31:-23:-648并且秒表不运行

代码如下所示:

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
#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.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
#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

对代码作了细微的修改。。但是它现在工作正常了…

我创建了一个具有暂停/恢复功能的简单秒表。它使用两个NSDate变量来跟踪暂停之间的时间:

NSTimeInterval secondsBetween = [pause_date timeIntervalSinceDate:start_date];
start_date = [NSDate dateWithTimeIntervalSinceNow:(-1)*secondsBetween];

查看此处的示例项目:

使用nstimer创建秒表使用此:nstimer*stopTimer;Alreadyth这将帮助你做一件事,为stopTimer分配属性和合成,并与self一起使用;到目前为止,我们已经解决了这个问题。。但当我再次按下start时,该值返回到零。