Вопрос

I would like to add the values in the strings together which are in the format:"HH:mm:ss.SSS" and display it back again. In my case, it is adding timeString with difference to get currentString. I am not sure of the correct way of doing it. I am definitely doing it wrongly. The problem i face is that when i click the stop button and click start again, it starts off from 0 again.. I would like it to start off from the same spot where it left off..

The portion of the code is here:

-(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.SSS"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
    timeString=[dateFormatter stringFromDate:timerDate];
    currentString=[dateFormatter stringFromDate:timerDate];
    //double doubleOfString = [timeString doubleValue] + [difference doubleValue];
    //currentString = [NSString stringWithFormat:@"%f",doubleOfString];
    lbl.text = currentString;
}

The entire code is here:

Viewcontroller.h

#import <UIKit/UIKit.h>

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

@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.000";
    running = FALSE;
    lap = 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;
        lap = 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;
        lap = 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.SSS"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
    timeString=[dateFormatter stringFromDate:timerDate];
    currentString=[dateFormatter stringFromDate:timerDate];
    double doubleOfString = [timeString doubleValue] + [difference doubleValue];
    //currentString = [NSString stringWithFormat:@"%f",doubleOfString];
    lbl.text = currentString;
}

-(IBAction)resetPressed:(id)sender{
    if (!lap) {
        [stopTimer invalidate];
        stopTimer = nil;
        tableItems = [[NSMutableArray alloc] init];
        startDate = [NSDate date];
        lbl.text = @"00.00.00.000";
        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"];
        //UIView *v = [[UIView alloc] init];
        //v.backgroundColor = [UIColor redColor];
        //cell.selectedBackgroundView = v;
        //changing the radius of the corners
        //cell.layer.cornerRadius = 10;

    }

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

    //Step 4: Return the row
    return cell;

}



@end

I welcome any other corrections that you can suggest...

Это было полезно?

Решение

I would kindly suggest not applying math to strings.

I mean:
What's the sum of the words “apple” and “Millenium Falcon”? Doesn’t make any sense, does it?

So why are you trying to form an addition of two strings, when you had a perfectly good number (timeInterval) — a type perfectly suitable to perform algebraic operations on?

PS:
NSDateFormatter instances want to be reused. Stash yours in an instance variable.

Другие советы

Your update timer function will be:Firstly add int counter in .h file . In viewDidLoad counter = 0;

 -(void)updateTimer{

  counter ++;
  int hours = counter / 3600 ;
  int minutes = (counter / 60) - (hours * 60); 
  int seconds = counter - (hours * 3600) - (minutes * 60);

  lbl.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top