-1

Hello friends I am new to iOS development. I have one issue related to NSTimer. I am using NSTimer in my view controller.
Timer starts decreasing value on the UIButton event. After calling timer when i goes to another view controller. Then timer decreasing value that it's work fine. But when i come back to timer view then timer stops updating value means timer don't call it's @selector() method. How can i call value updating method when i come back to timer view ?

My code is as follows:

-(IBAction)btnStrDecPressed:(id)sender
{

    countDownlabel.text = [NSString stringWithFormat:@"%@:%@:%@", pickhour, pickmins, picksecs];
    secondsLeft=[pickhour intValue] * 3600;
    secondsLeft=secondsLeft + [pickmins intValue] * 60;
    secondsLeft=secondsLeft + [picksecs intValue];
    appdel.LeftSeconds=secondsLeft;
    NSLog(@"%d",appdel.LeftSeconds);


   if(timer==nil)
   {
       timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES];
   }
 }

-(void)updateCountdown
{
  int hours, minutes, seconds;

  hours = appdel.LeftSeconds / 3600;
  minutes = (appdel.LeftSeconds % 3600) / 60;
  seconds = (appdel.LeftSeconds %3600) % 60;
  appdel.LeftSeconds--;
  countDownlabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];

  if (appdel.LeftSeconds==0)
  {
      [[[UIAlertView alloc] initWithTitle:@"Restaurant" message:@"Timer Completed" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
      [timer invalidate];
  }
}

Where pickhour,pickmins,picksecs are values getting from UIPickerview.

Pradhyuman sinh
  • 3,936
  • 1
  • 23
  • 38
  • Show us your code in the viewWillAppear viewWillDisapear methods, also show us how are you navigating to the other viewController – Ecarrion Aug 02 '13 at 09:14
  • I don't add viewWillAppear and viewDidAppear method only i have viewDidLoad. And I have slider view like master-detail view. Like on click of master view's didSelectRow we change detail view's info. Here i push another view on click of master view's didSelectRow. I'm using sample code for that. – Pradhyuman sinh Aug 02 '13 at 09:20
  • Hi, Are you sure? you btnStrDecPressed function calling after button pressed... if yes, then check control enter in if(timer==nil) condition. because i think, your timer is not initialize. – Hindu Aug 02 '13 at 09:31
  • Is `[timer invalidate];` being run (and the alert shown) while the other view is on screen? – Wain Aug 02 '13 at 09:37
  • Every time when i come back from another view then i get timer=nil, so control enters in if(timer==nil) condition. But after that my timer decrements the seconds value by 2. if i get back from another view to timer view 3rd time then it starts decrement timer value by 3 secs. – Pradhyuman sinh Aug 02 '13 at 09:38

3 Answers3

1

Use the following code :

-(void)viewWillAppear:(BOOL)animated
{
    timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES];
}

-(void)updateCountdown
{
  NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
  int hours, minutes, seconds;
  if([def valueForKey:@"time"] != nil)
  {
     countDownlabel.text = [def valueForKey:@"time"];
  NSArray *arr = [countDownlabel.text componentsSeparatedByString:@":"];
  hours = [arr objectAtIndex:0];
  minutes = [arr objectAtIndex:1];
  seconds = [arr objectAtIndex:2];
  appdel.LeftSeconds--;
  }
  else{
  hours = appdel.LeftSeconds / 3600;
  minutes = (appdel.LeftSeconds % 3600) / 60;
  seconds = (appdel.LeftSeconds %3600) % 60;
  appdel.LeftSeconds--;
  }
  countDownlabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
  [def setValue:[NSString stringWithFormat:@"%@",countDownlabel.text] forKey:@"time"];
  if (appdel.LeftSeconds==0)
  {
      [[[UIAlertView alloc] initWithTitle:@"Restaurant" message:@"Timer Completed" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
      [timer invalidate];
  }
}

(Note: Use the NSUserdefault value to store the time and when the view appear the default stored value would be fetched)

Raviraj Jadeja
  • 849
  • 6
  • 17
  • every time it goes in the if([def valueForKey:@"time"] != nil). At first time it takes garbage value and enters in this condition. – Pradhyuman sinh Aug 02 '13 at 10:43
  • Do one thing remove the != nil condition only keep it as [def valueForKey]. But the point is you will only get the time if you save it. – Raviraj Jadeja Aug 02 '13 at 11:20
0

The timer is ticking all of the time until your [timer invalidate] - not just when you can see the view.

The reason you can't re-initialise it is because you don't reset the timer completely. Try:

[timer invalidate];
timer = nil;
Dominic
  • 258
  • 2
  • 8
0

The problem is that every time you leave the timer view controller and come back you create a new instance of it. That's why the timer is nil every time and it decrements by 1 more second every time you start the timer.

I suggest you move the updateCountdown selector and timer to the AppDelegate.

UpL1nK
  • 559
  • 1
  • 6
  • 14