2012-11-01 53 views
3

我正在嘗試製作秒錶應用程序,並面臨使其正常工作的問題。當我啓動秒錶並停止並再次開始時,它不會從停止的地方繼續。它繼續奔跑,好像它沒有停下來一樣。需要一些指導才能正常工作。因爲上午我一直在努力,我已經作出類似蘋果的秒錶功能的休息,只有這是竊聽我。感謝所有幫助...Iphone秒錶 - 啓動,停止和啓動,但不會從停止的位置開始

的代碼看起來是這樣的:

的ViewController .H

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{ 
    UILabel *lbl; 
    NSTimer *stopTimer; 
    NSDate *startDate,*currentDate; 
    BOOL running; 
    UIButton *bttn; 
    NSMutableArray *tableItems; 
    NSString *timeString,*currentString; 
    UITableView *tableview; 
    int counter; 
} 

@property (strong,nonatomic) IBOutlet UILabel *lbl; 
@property (strong,nonatomic) IBOutlet UIButton *bttn; 
@property (strong,nonatomic) NSMutableArray *tableItems; 
@property (strong,nonatomic) NSString *timeString; 
@property (strong,nonatomic) IBOutlet UITableView *tableview; 

-(IBAction)startPressed:(id)sender; 
-(IBAction)resetPressed:(id)sender; 

-(void)updateTimer; 

@end 

ViewController.m

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 
@synthesize lbl,bttn,tableItems,timeString,tableview; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    lbl.text = @"00.00.00.0"; 
    running = FALSE; 
    //difference = @"0"; 
    startDate = [NSDate date]; 
    tableItems = [[NSMutableArray alloc] init]; 
} 

- (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]; 
     [bttn setTitle:@"Lap" forState:UIControlStateNormal]; 
     if (stopTimer == nil) { 
      stopTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0 
                 target:self 
                 selector:@selector(updateTimer) 
                 userInfo:nil 
                 repeats:YES]; 
     } 
    }else{ 
     running = FALSE; 
     //startDate = currentDate; 
     //difference = currentString; 
     [sender setTitle:@"Start" forState:UIControlStateNormal]; 
     [bttn setTitle:@"Restart" forState:UIControlStateNormal]; 
     [stopTimer invalidate]; 
     stopTimer = nil; 
    } 

} 
-(void)updateTimer{ 
    currentDate = [NSDate date]; 
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate]; 
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval]; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"HH:mm:ss.S"]; 
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]]; 
    timeString=[dateFormatter stringFromDate:timerDate]; 
    lbl.text = timeString; 
} 

-(IBAction)resetPressed:(id)sender{ 
    if (!running) { 
     [stopTimer invalidate]; 
     stopTimer = nil; 
     tableItems = [[NSMutableArray alloc] init]; 
     startDate = [NSDate date]; 
     lbl.text = @"00.00.00.0"; 
     running = FALSE; 
    } 
    else{ 
     [tableItems insertObject:timeString atIndex:0]; 
     [tableview reloadData]; 
    } 

} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return tableItems.count; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    //Step 1:Check whether if we can reuse a cell 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 

    //Step2: If there are no new cells to reuse,create a new one 
    if(cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];   
    } 

    //Step 3: Set the cell text content 
    cell.textLabel.text = [tableItems objectAtIndex:indexPath.row]; 

    //Step 4: Return the row 
    return cell; 

} 



@end 

回答

2

在你updateTimer方法你計算之間的時間差0和當前日期。你不會考慮到你將不得不減去秒錶停止時的時間。

編輯:

我建議總結和保存啓動之間通過所有timeIntervals和停止

屬性NSTimeInterval timePassed添加到您的類,並修改你的代碼是這樣的:

- (void)viewDidLoad 
{ 
    //... 
    timePassed = 0; 
} 

-(IBAction)startPressed:(id)sender{ 
    if(!running){ 
     //... 
     startDate = [NSDate date]; 
     //... 
    }else{ 
     NSDate *currentDate = [NSDate date]; 
     NSTimeInterval intervalToAdd = [currentDate timeIntervalSinceDate:startDate]; 
     timePassed += intervalToAdd; 
    //... 
} 

-(void)updateTimer{ 
    currentDate = [NSDate date]; 
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate]; 

    timeInterval += timePassed; // <<<<< 

    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval]; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"HH:mm:ss.S"]; 
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]]; 
    timeString=[dateFormatter stringFromDate:timerDate]; 
    lbl.text = timeString; 
} 

-(IBAction)resetPressed:(id)sender{ 
    if (!running) { 
     //... 
     timePassed = 0; 
     //... 
    } 
    //... 
} 
+0

我面臨的一個問題是:startDate,currentDate是字符串格式。不能只是添加他們.. – lakesh

+0

最好的是你顯示代碼以及.. – lakesh

+0

我已經更新了我的答案 – Tobi

0

在我的活動項目中引用一些代碼。

NSDate *timeStart = [NSDate dateWithTimeIntervalSince1970:0]; 
NSDate *timeEnd = [timeStart dateByAddingTimeInterval:_timePassed]; 

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"00:00:00"]; 
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]]; 
NSString *strDate = [dateFormatter stringFromDate:timeEnd]; 

_timeLabel.text = strDate; 

這就是我做我的。如果你想使用UILabel作爲秒錶, MZTimerLabel是一款非常棒的雙線解決方案。看一看。

乾杯。