Iphone 单击主页按钮时在目标C中计时

Iphone 单击主页按钮时在目标C中计时,iphone,objective-c,ios5,nstimer,Iphone,Objective C,Ios5,Nstimer,我在ViewController类中有NSTimer。。NStimer的所有方法都在该类中。。我的NSTimer在播放和暂停时工作正常。。。当我按下iPhone上的home按钮时,计时器也正常工作(当应用程序进入前台时,它从应用程序进入后台开始运行)。。。在我的应用程序中,当我的应用程序进入前台时,我会快速发出警报(应用程序中的UIAlertview)。在屏幕上显示警报时,我的NSTimer正在运行(未对警报做出响应)。。我想停止NSTimer,直到用户响应我的警报。。。之后,我想启动NSTim

我在ViewController类中有NSTimer。。NStimer的所有方法都在该类中。。我的NSTimer在播放和暂停时工作正常。。。当我按下iPhone上的home按钮时,计时器也正常工作(当应用程序进入前台时,它从应用程序进入后台开始运行)。。。在我的应用程序中,当我的应用程序进入前台时,我会快速发出警报(应用程序中的UIAlertview)。在屏幕上显示警报时,我的NSTimer正在运行(未对警报做出响应)。。我想停止NSTimer,直到用户响应我的警报。。。之后,我想启动NSTimer。。。我该怎么做。。。?请帮帮我。。。先谢谢你。。。我想从Appdelegate.m文件调用NSTimer方法。。。我正确地调用这些方法。。。并调用ViewController中的方法。。但是这个动作没有表现出来。。。对于从viewController类调用的相同方法,它正在工作

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.
 */
}
有几件事:

1) 它看起来像一个不完整的单例设计。如果你真的想要一个单身汉,请参考帖子

2) 无需将stopwatchtimer添加到当前运行循环。scheduledTimerWithTimeInterval方法为您安排时间

3) 我不知道在哪里声明秒表计时器,但请确保将其声明为保留属性(或ARC中的强属性)

4) 要停止计时器,只需调用
[stopwatchtimer invalidate]
要重新启动它,请使用最初调用的scheduledTimerWithTimeInterval类方法重新实例化并覆盖旧的

5) 要在警报视图完成时执行任何操作,请实现委托方法:

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

因此,您可以在显示警报之前使计时器无效,然后在收到警报完成通知时重新生成计时器。

我已编辑了我的问题。。请看一下,请包括适用的代码,以便我们可以提供帮助!我已经包括了代码。。请检查一下……是的。。。这里真的不需要代码。正如danh指出的那样,使用UIAlertView委托方法。我看不出您在发布的代码中停止了它。您需要在[alert show]之前调用invalidate或“OnStopped”。试着在节目开始前在线上这样做。它会停止的。如果你仍然看到它着火,这意味着你有两个或更多的定时器。在构建断点的位置设置断点。您应该只命中该断点一次。祝你好运