0

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 
Laury
  • 53
  • 11
  • You are trying to show timer1 instead of timeNow. – oxigen Feb 06 '15 at 10:10
  • It seems like it would be simpler to usr something like CFAbsoluteTime runStartTime = CFAbsoluteTimeGetCurrent ();, then when you want to print the elapsed time: CFTimeInterval runTime = CFAbsoluteTimeGetCurrent () - runStartTime; This will give you the elapsed time in seconds. No need for timer or timerTick. – jwlaughton Feb 06 '15 at 10:16
  • I started coding in objective-c 2 weeks ago and not familiar with some of the methods :) – Laury Feb 06 '15 at 10:20

2 Answers2

0

The timer value is stored in timeMin and timeSec. You just have to get the timeMin and timeSec values to get your timer value.

NSString * resultTimer = [NSString stringWithFormat:@"%02d:%02d", timeMin, timeSec];
NSLog(@"%@",resultTimer);

EDIT: You can also implement a method stringValue :

-(NSString*)stringValue{ 
    return [NSString stringWithFormat:@"%02d:%02d", timeMin, timeSec]; 
}

And call the method

[yourtimer stringValue];
Sudo
  • 991
  • 9
  • 24
  • Do i initialize it in the game file or results file? – Laury Feb 06 '15 at 10:15
  • Wherever you want to display it. You can also implement a new method that returns the string of your timer. `-(NSString*)stringValue{ return [NSString stringWithFormat:@"%02d:%02d", timeMin, timeSec]; }` and call `[yourtimer stringValue];` – Sudo Feb 06 '15 at 10:25
  • I tried to implement your first example but it showed only 00:00 even when the timer was 00:10 when i pressed continue – Laury Feb 06 '15 at 10:41
  • In your .h file, write the prototype of the method `stringValue` (in my comment above) In your .m file, implement the method `stringValue`. Wherever you want to have the value of your timer, call the method `stringValue`. For example, `pointsInfo.text = [NSString stringWithFormat:@"¡Felicidades! ¡Has conseguido la máxima puntuación: %@! Debajo puedes encontrar las respuestas que has seleccionado:, time results: %@ ",sc, [timer1 stringValue]];` – Sudo Feb 06 '15 at 11:23
  • I get error saying : No visible @interface for NS... Did i do something wrong? by prototypr uou mean something like this in .h file? -(NSString *)stringValue; – Laury Feb 06 '15 at 11:36
  • you should read this http://stackoverflow.com/questions/10387330/no-visible-interface-for – Sudo Feb 06 '15 at 11:41
  • Yeh still cant fix it ;( – Laury Feb 06 '15 at 11:55
0

Create a global variable

@interface YourViewController ()
{
NSString *timeNow;
}

Now use this in your timerTick method.

timeNow = [NSString stringWithFormat:@"%02d:%02d", timeMin, timeSec];

At the end, display score as

pointsInfo.text = timeNow;
Fawad Masud
  • 12,219
  • 3
  • 25
  • 34
  • hmm how does this change anything? plss explain – Laury Feb 06 '15 at 11:03
  • Your label will change every second (whenever timerTick is called ). This is what you wanted. – Fawad Masud Feb 06 '15 at 11:16
  • man i already said i have timer working, my question was how to get the result, as in how long it took me to complete the level and be displayed in result scene – Laury Feb 06 '15 at 11:20
  • timerTick method is initialized in Game1.m and pointsInfo is in game1Results.m, I initialized timeNow in game1.h. I tried doing like you showed but it returns black, time results are not showed. – Laury Feb 09 '15 at 08:47
  • what is Game1? a view controller? and game1Results? Please post the methods in game1Results where Game1 is called. And method in Game1 where you are trying to pass time value to game1Result. – Fawad Masud Feb 09 '15 at 08:57
  • I updated my question. i hope this helps and if not i can add more – Laury Feb 09 '15 at 09:12
  • I will advise you to create the timer functions in Game1Results instead of Game1. If you do not want to do this, use delegate methods to pass value of score from Game1 to Game1Results. Simple inheritance will not get score from Game1 which you are doing now (@interface Game1Results : Game1). – Fawad Masud Feb 09 '15 at 09:57
  • At game1 i want the user to be able to see the timer running and after it finishes i want to to be displayed in the results. Im still new to this and im not sure how to use delegate methods yet. – Laury Feb 09 '15 at 10:10
  • Delegates are very easy. You must learn it if you are developing for iOS there are many good tutorials . Also check this http://stackoverflow.com/questions/6168919/how-do-i-set-up-a-simple-delegate-to-communicate-between-two-view-controllers – Fawad Masud Feb 09 '15 at 10:17