Question

I'm developing an app which tracks distance time etc. I've just implemented a method for tracking the total time using NSTimer, which works exactly the way I want it to. But I need to convert the total time into hours so I can use it to calculate calories and average speed but i'm having trouble with it!

Here is my timer method:

- (void) startStopWatch {
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1
                                                      target:self
                                                    selector:@selector(timerController)
                                                    userInfo:nil
                                                     repeats:YES];
}

- (void)timerController {
    _seconds++;
    NSLog(@"seconds = %zd", _seconds);
    if (_seconds <10){
        NSString *secondsString = [NSString stringWithFormat:@"0%li", (long)_seconds];
        _SecondsLabel.text = secondsString;
    }
    else{
        NSString *secondsString = [NSString stringWithFormat:@"%li", (long)_seconds];
        _SecondsLabel.text = secondsString;
    }

    if (_seconds == 60){
        _minutes++;
        _seconds = 00;
        NSString *secondsString = [NSString stringWithFormat:@"0%li", (long)_seconds];
        _SecondsLabel.text = secondsString;
        if (_minutes <10){
            NSString *minutesString = [NSString stringWithFormat:@"0%li", (long)_minutes];
            _minutesLabel.text = minutesString;
        }
        else {
            NSLog(@"minutes = %zd", _minutes);
            NSString *minutesString = [NSString stringWithFormat:@"%li", (long)_minutes];
            _minutesLabel.text = minutesString;
        }
    }
    if (_minutes == 60){
        self.hoursLabel.hidden = NO;
        _hours++;
        _minutes = 0;
        NSString *minutesString = [NSString stringWithFormat:@"%li", (long)_minutes];
        _minutesLabel.text = minutesString;
        NSString *hoursString = [NSString stringWithFormat:@"%li", (long)_hours];
        _hoursLabel.text = hoursString;

    }
    [self UpdateTotalTime];
}

and here is my method to convert seconds and minutes into hours:

- (void) UpdateTotalTime {
    _secondsToHours = _seconds / 3600;
    _minutesToHours = _minutes / 60;
    _totalTime = (_secondsToHours + _minutesToHours + _hours);
     NSLog(@"totaltime = %zd", _totalTime);
}

The problem is that nothing is being added to _totalTime, any ideas? Also, I'm very new to Objective-C so I may be missing something very basic! Thanks

Était-ce utile?

La solution

All your variables must be doubles, otherwise integer division is performed (the result is 0), and converted and assigned to a double variable (0.000).

Autres conseils

Just as an example of how the code can be reduced and made more readable:

- (void)timerController {
    _seconds++;
    NSLog(@"seconds = %zd", _seconds);

    if (_seconds == 60) {
        _minutes++;
        _seconds = 00;
    }
    if (_minutes == 60) {
        self.hoursLabel.hidden = NO;
        _hours++;
        _minutes = 0;
        _hoursLabel.text = [NSString stringWithFormat:@"%li", (long)_hours];
    }
    _minutesLabel.text = [NSString stringWithFormat:@"%02li", (long)_minutes];
    _secondsLabel.text = [NSString stringWithFormat:@"%02li", (long)_seconds];
    [self UpdateTotalTime];
}

As an alternate, just use _totaTime:

- (void)timerController {
    _totalTime++;
    NSLog(@"seconds = %zd", _totalTime);

    int seconds =  _totalTime % 60;
    int minutes = (_totalTime / 60) % 60;
    int hours   =  _totalTime / 3600;

    if (hours) {
        self.hoursLabel.hidden = NO;
        _hoursLabel.text = [NSString stringWithFormat:@"%i", hours];
    }
    _minutesLabel.text = [NSString stringWithFormat:@"%02i", minutes];
    _secondsLabel.text = [NSString stringWithFormat:@"%02i", seconds];
}

An answer as opposed to a comment for formatting. No logic errors were corrected.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top