I have added a simple timer in my app. At the moment the timer is just going from 00:00, 00:01, 00:02 and so on. When i complete a level i got to result screen which displays my score. Im still quite new to this and i was wondering how can i display the timer results. This is the implementation of the timer:
//in .h file
IBOutlet UILabel *GameTimer;
int timeSec;
int timeMin;
NSTimer *timer;
//in .m file
-(void)startTimer1{
//Invalidate function stops 'timer1' before it is restarted
//initializing 'timer1'
timer1 = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(timerTick:)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer1 forMode:NSDefaultRunLoopMode];
}
//Event called every time the NSTimer ticks.
- (void)timerTick:(NSTimer *)timer1
{
timeSec++;
if (timeSec == 60)
{
timeSec = 0;
timeMin++;
}
//Format the string 00:00
NSString* timeNow = [NSString stringWithFormat:@"%02d:%02d", timeMin, timeSec];
//Display on your label
//[timeLabel setStringValue:timeNow];
GameTimer.text= timeNow;
}
My attempt to display it on results screen but all i get is 0(dont mind the Spanish string just says how many points you got):
pointsInfo.text = [NSString stringWithFormat:@"¡Felicidades! ¡Has conseguido la máxima puntuación: %@! Debajo puedes encontrar las respuestas que has seleccionado:, time results: %@ ",sc, timer1];
EDIT: Game1 is a viewController and so is gameResults1. Game1 method: // Method for starting 'timer1' -(void)startTimer1{
//Invalidate function stops 'timer1' before it is restarted
//initializing 'timer1'
timer1 = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(timerTick:)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer1 forMode:NSDefaultRunLoopMode];
}
//Event called every time the NSTimer ticks.
- (void)timerTick:(NSTimer *)timer1
{
timeSec++;
if (timeSec == 60)
{
timeSec = 0;
timeMin++;
}
//Format the string 00:00
timeNow = [NSString stringWithFormat:@"%02d:%02d", timeMin, timeSec];
//Display on your label
//[timeLabel setStringValue:timeNow];
GameTimer.text= timeNow;
}
//Call this to stop the timer event(could use as a 'Pause' or 'Reset')
- (void) StopTimer
{
[timer invalidate];
timeSec = 0;
timeMin = 0;
//Since we reset here, and timerTick won't update your label again, we need to refresh it again.
//Format the string in 00:00
timeNow = [NSString stringWithFormat:@"%02d:%02d", timeMin, timeSec];
//Display on your label
// [timeLabel setStringValue:timeNow];
GameTimer.text= timeNow;
}
// @selector for 'timer1'
I dont think i have any particular way in calling game1 in gameresults1. other than:
@interface Game1Results : Game1