-2

I have NSTimer in ViewController Class.. And all the methods for NStimer are in that class.. My NSTimer is working properly for play and pause... When i press home button on iPhone also the timer is working properly(it starts running from the time where the application enters into background when the application enters into foreground)... In my application i am rising an alert quickly when my application enters into foreground(UIAlertview in application didEnterForeGround). Here my NSTimer is running when the alert is on the screen(didn't give response to alert).. I want stop the NSTimer Upto the User responding to my alert... After that I want to start the NSTimer... How Can i do that...? Please help me... Thanks in Advance... I want call the NSTimer methods from Appdelegate.m file... Iam calling these methods properly... And the methods in ViewController are called.. But the action is Not Performing... For the Same method when call from viewController class it is working...

enter code here

ViewController.h

+(ExamViewController *)evcInstance;

ViewController.m

ExamViewController *evc = nil;
@implementation ExamViewController

+(ExamViewController *)evcInstance
{
if(!evc) 
{
    evc = [[ExamViewController alloc] init];
}
return evc;
}




- (void)onStartPressed
{
stopwatchtimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:stopwatchtimer forMode:NSRunLoopCommonModes]; 
resume.hidden = YES;
resume.enabled=NO;
pause.hidden = NO;
pause.enabled=YES;
} 


- (void)onStopPressed
{


[stopwatchtimer invalidate];
stopwatchtimer = nil;
pause.hidden = YES;
pause.enabled=NO;
resume.hidden = NO;
resume.enabled=YES;
}

Appdelegate.m

- (void)applicationWillEnterForeground:(UIApplication *)application
{
[[ExamViewController evcInstance] onStopPressed];

NSLog(@"applicationWillEnterForeground");

if(viewCalled ==YES)
{

    UIAlertView *alertview=[[UIAlertView alloc]initWithTitle:@"" message:@"DO! You Want To continue" delegate:self cancelButtonTitle:@"YES" otherButtonTitles:@"NO", nil];

    [alertview show];

    [alertview release];



}
/*
 Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
 */
}
SriKanth
  • 459
  • 7
  • 27

1 Answers1

1

a few things:

1) It looks like an incomplete singleton design. If you really want a singleton, refer to a post like this one.

2) No need to add stopwatchtimer to the current run loop. The scheduledTimerWithTimeInterval method schedules it for you.

3) I don't see where you declare stopwatch timer, but be sure to declare it as a retained property (or strong in ARC).

4) To stop the timer, just call [stopwatchtimer invalidate]; To restart it, re-instantiate and overwrite the old one using the same scheduledTimerWithTimeInterval class method that you called originally.

5) To do anything when an alert view completes, implement the delegate method:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

So you can invalidate the timer before presenting the alert, then rebuild it when you get notified the alert is finished.

Community
  • 1
  • 1
danh
  • 62,181
  • 10
  • 95
  • 136
  • I don't see that you stop it in the code you posted. You would need a call to invalidate or " onStopPressed" before [alert show]. Try doing this on the line just before show. It will stop. If you still see it fire, it means you have two or more timers. Put a breakpoint on where you build it. You should only hit that breakpoint once. Good luck. – danh May 30 '12 at 13:50