Big Box Developer
Big Box Developer

Reputation: 137

NSTimer shows UILabel and UIImageView when it stops

In my app project I want to be able to tap a button, wait 5 seconds, and then a UILabel and UIImageView appear. Here's what I have so far:

- (IBAction)startTimer
{
    responseBox.placeholder = @"Message Here";
    timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(timerStop) userInfo:nil repeats:YES];
    mainInt +=1;
}

- (void)timerStop
{
    if (mainInt == 5)
    {
        [timer invalidate];
        titleLabel.hidden = NO;
        messageLabel.hidden = NO;
        messageLabel.text = @"Message";
        image.hidden = NO;
    }
}

Everything's linked correctly, what's going on?

Upvotes: 0

Views: 520

Answers (1)

WrightsCS
WrightsCS

Reputation: 50717

mainInt is only incremented when you press the startTimer button, so that does you no good in timerStop. Also, if you want to increment mainInt correctly, use mainInt++;

Try this:

- (IBAction)startTimer
{
    responseBox.placeholder = @"Message Here";
    timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(timerStop) userInfo:nil repeats:YES];
    mainInt++;
}

- (void)timerStop
{
    [timer invalidate];
    titleLabel.hidden = NO;
    messageLabel.hidden = NO;
    messageLabel.text = @"Message";
    image.hidden = NO;
}

Upvotes: 1

Related Questions