George Friday
George Friday

Reputation: 585

NSTimer for UILabel

I have a question about using NSTimer in combination with a label. I have not been able to find the answer to this yet, but this is probably elementary. I am still learning iOS...

My app is a calculator that contains a "Mode" button. By default, my calculator starts in Mode 1 with the label "messageText" stating "No Errors." When the mode button is pressed, I would like UILabel "messageText" to display the text "Mode 2" for 3 seconds, before it changes back to "No Errors."

I am not sure how to integrate the NSTimer with my label. Here is what I have so far. My problem is that the variable *timer is unused. How do I use this in conjunction with only my messageText label, and not the others? Any help would be greatly appreciated:

-(IBAction)mode
{
years.text = @"0";
months.text = @"0";
days.text = @"0";

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:3 target: self selector:@selector(timerEnded) userInfo: nil repeats:NO];
messageText.text = @"Mode 2";

}

-(void)timerEnded
{
    messageText.text = @"No Errors";
}

Upvotes: 0

Views: 179

Answers (1)

rmaddy
rmaddy

Reputation: 318954

You declare and initialize a variable named timer but you never use it.

To eliminate the warning either use the variable elsewhere or don't create the variable. Just do:

[NSTimer scheduledTimerWithTimeInterval:3 target: self selector:@selector(timerEnded) userInfo: nil repeats:NO];

If you might ever need to invalidate (stop) the timer, you should keep a reference in an ivar so you can call invalidate on it.

Upvotes: 2

Related Questions