anyName
anyName

Reputation: 113

Objective C - Continue NSTimer after popping previous page

I have an NSTimer in a view controller which starts counting when the user presses a button. It counts down seconds for 3 minutes. What I want is that if the user wants to go back previous page (maybe go another page) and come back to this timer page, he/she should see counting timer label. I mean, I know timer continues to counting but when I load this page again, I cannot see its label as i left. Also I check my timer and get it's not considered as "valid".

I don't want to use AppDelegate for this purpose because I have many view controllers and this one is not that important. So, how can I achieve this continuous timer label? Should I use static timer or flag?

Thank you.

Upvotes: 0

Views: 244

Answers (2)

Tobi Nary
Tobi Nary

Reputation: 4596

Making the timer static might introduce nasty memory problems as it holds a reference to it's target.

But as that already is no problem for you seemingly, that'd be a good way to go. Better yet: create a singleton object for your timer that encapsulaten the NSTimer and the value it's counting. This way, your view controllers can use KVO on the value and are otherwise completely independent of the timer.

Upvotes: 0

Sucharu Hasija
Sucharu Hasija

Reputation: 1126

you cna implement a

static int counter  = 0 ;

by having a selector method in the NSTimer and use that counter to continue the counting

[NSTimer scheduledTimerWithTimeInterval:180.0f 
                                 target:self 
                               selector:@selector(updateTime:) 
                               userInfo:nil 
                                repeats:YES];  
// Implement Method
-(void)updateTime: (NSTimer *) timer {

   counter++;
self.label.text  = counter;
} 

Upvotes: 1

Related Questions