0

This is not all the code. I have made sure that I have declared everything correctly however the label is not changing as 'seconds' is decreasing.

I'm not sure why as in 'subtractTime' I have made timerLabel.text equal to the string with format using seconds which "should" and is counting down as I use an alert to reset the game and so even though the label isn't changing, I know it is counting down otherwise the alert wouldn't be triggered from 'seconds' equalling 0.

- (void)setupGame;
{
    seconds = 30;
    count = 0;

    timerLabel.text = [NSString stringWithFormat: @"Time: %i", seconds];
    scoreLabel.text = [NSString stringWithFormat: @"Score: %i", count];

    timer = [NSTimer scheduledTimerWithTimeInterval:1.0
         target:self
         selector:@selector(subtractTime)
         userInfo:nil
         repeats:YES];

}
- (void)subtractTime;
{
    seconds--;
    timerLabel.text = [NSString stringWithFormat:@"Time: %i", seconds];

    if (seconds == 0)
    {
        [timer invalidate];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Time is up !" 
                          message: [NSString stringWithFormat: @"You Scored %i points", count]
                          delegate:self
                          cancelButtonTitle: @"Play Again"
                          otherButtonTitles:nil];

        [alert show];
    }

}
@end
user2602189
  • 11
  • 1
  • 3

1 Answers1

0

Make sure in Storyboard/Interface builder that your label is wired properly to the viewXontroller's .h file. A good way to test this is to "prime" your label on the XIB with something other than "Time:" but instead with something nonsensical like "foo". That way you'll be able to accurately tell if the label is changing at all versus troubleshooting whether or not your Int value isn't showing properly.

Dan
  • 5,153
  • 4
  • 31
  • 42
  • Add `assert(timerLabel != nil, @"timerlabel nil!"); – Rob van der Veer Jul 20 '13 at 16:59
  • What does that do Rob? – user2602189 Jul 20 '13 at 17:03
  • In your project toggle the Assistant View (it looks like a little tuxedo icon towards the top-right of the XCode window). In the left pane open your XIB/Storyboard and in the right-pane open your viewController's .h file. Right-click and drag from your Label in the XIB to the declaration for the Label in the .h file. – Dan Jul 20 '13 at 17:08
  • Thanks D80 it fixed my issue as I didnt have it correctly linked =] Nooby mistake. – user2602189 Jul 20 '13 at 17:17
  • 1
    @user2602189: use asserts to verify state of variables. If the assertion fails, your program will stop. Assertions help with debugging. Even better, use NSAssert. http://stackoverflow.com/a/2003432/2462469 – Rob van der Veer Jul 20 '13 at 18:14