Muhammad Umar
Muhammad Umar

Reputation: 11782

Images not getting changed with NSTimer

This is my code

- (void)updateCounterImage:(NSTimer *)theTimer
{
    static int count = 0;
    count += 1;
    int crb = 6 - count;
    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"ipad_game_timer_digit-%d.png", crb]];

    if ( count == 6)
        [timer release];

    [countTime setImage:image];
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    correctLevel.hidden = YES;
    incorrectLevel.hidden = YES;
    incorrectAnswer.hidden = YES;
    correctAnswer.hidden = YES;
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
                                     target:self
                                   selector:@selector(updateCounterImage:)
                                   userInfo:nil
                                    repeats:NO];

}

#import <UIKit/UIKit.h>

@interface GameController : UIViewController
{

    IBOutlet UIImageView *countTime;
    NSTimer *timer;
}


@end

The problem is my imageView is not changing its image for countime.

Upvotes: 0

Views: 107

Answers (2)

Maciej Trybiło
Maciej Trybiło

Reputation: 1207

You have set repeats to NO in the scheduledTimerWithTimeInterval which means the timer will only tick once.

Upvotes: 1

Ivan Dyachenko
Ivan Dyachenko

Reputation: 1348

You should write

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

Upvotes: 1

Related Questions