-2

ViewController クラスに NSTimer があります。そして、NStimer のすべてのメソッドはそのクラスにあります。アプリケーションがフォアグラウンドに入ったときにアプリケーションがバックグラウンドに入る時間)...私のアプリケーションでは、アプリケーションがフォアグラウンドに入るとすぐにアラートが発生しています(アプリケーションのUIAlertview didEnterForeGround)。ここで、アラートが画面に表示されているときに NSTimer が実行されています (アラートに応答しませんでした)。アラートに応答しているユーザーまで NSTimer を停止したい...その後、NSTimer を開始したい...方法それをしてもいいですか...?助けてください...事前に感謝します...Appdelegate.mファイルからNSTimerメソッドを呼び出したい...これらのメソッドを適切に呼び出しています...

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.
 */
}
4

1 に答える 1

1

いくつかのこと:

1) 不完全なシングルトン デザインのように見えます。本当にシングルトンが必要な場合は、このような投稿を参照してください。

2) 現在の実行ループにストップウォッチタイマーを追加する必要はありません。スケジュールされたTimerWithTimeInterval メソッドは、それをスケジュールします。

3)ストップウォッチタイマーを宣言する場所がわかりませんが、必ず保持プロパティ(またはARCで強力)として宣言してください。

4) タイマーを停止するには、単に呼び出します[stopwatchtimer invalidate]; 。再起動するには、最初に呼び出した同じ ScheduledTimerWithTimeInterval クラス メソッドを使用して、古いタイマーを再度インスタンス化し、上書きします。

5) アラート ビューが完了したときに何かを行うには、delegate メソッドを実装します。

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

そのため、アラートを表示する前にタイマーを無効にし、アラートが終了したという通知を受け取ったときにタイマーを再構築できます。

于 2012-05-30T05:03:11.510 に答える