Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7899375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:41:08+00:00 2026-06-03T08:41:08+00:00

I have my timer being called from the viewDidLoad . For the first time

  • 0

I have my timer being called from the viewDidLoad. For the first time when the timer calls the selector, the expected result is fine. But gradually when the calls are made, the function is somehow called multiple times by the selector. I logged a NSLog which shows that the number of outputs are increasing. I have the code below. Hope it makes the situation clear.

-(void)viewDidLoad
{
    remainingTicks = 10;
    [self updateLabel];
    myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
                       target:self
                       selector:@selector(handleTimerTick)
                       userInfo:nil
                       repeats:YES];
}

-(void)handleTimerTick
{
    remainingTicks--;
    [self updateLabel];
    if (remainingTicks <= 0) {
        [myTimer invalidate];
        myTimer = nil;
        UIButton *but = [[UIButton alloc] init];
        if (answerAt == 0) {
            [buttonA setBackgroundColor:[UIColor greenColor]];
        }
        else if (answerAt == 1) {
            [buttonB setBackgroundColor:[UIColor greenColor]];
        }
        else if (answerAt == 2) {
            [buttonC setBackgroundColor:[UIColor greenColor]];
        }
        else {
            [buttonD setBackgroundColor:[UIColor greenColor]];
        }

        [self performSelector:@selector(next:) withObject:but afterDelay:1.5 ];       
    }
}

-(void)updateLabel
{
    timerLabel.text = [[NSNumber numberWithUnsignedInt: remainingTicks] stringValue];
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T08:41:10+00:00Added an answer on June 3, 2026 at 8:41 am

    ViewDidLoad can be called multiple times during the lifetime of a UIViewController (for example if you get a memory warning and your view is not visible, the controller will release it and reload it next time it’s needed). To handle this behavior properly you should either:

    A) Test whether the timer exists before creating one (e.g. if (myTimer == nil) { /* initialize the timer */ } )

    or

    B) Clear the timer in your viewDidUnload method. (This is probably more along the lines of what you want since you most likely don’t want the timer to fire events related to an invisible view).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web service in ASP.NET being called by a time-sensitive process. If
I have a variable that's being changed all the time in the course of
I have an XML object being returned, and inside is a date and time
This question has been answered. The problem was that myEventMoveTrainManaul() was being called from
I have a UITableView where numberOfSectionsInTableView and numberOfRowsInSection are being called twice, with scope
If using Haskell as a library being called from my C program, what is
I have a service which can be called from Activities in an app. The
I'm running into a situation where didReceiveMemoryWarning is being called but viewDidUnload is not.
How can i have timer which runs for every 5 secs continously. below is
I have a timer that needs to not process its elapsed event handler at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.