2011-08-12 56 views
0

我想在屏幕上顯示警報時暫停屏幕,然後在警報解除時撤消暫停。我會怎麼做? 這裏是我的視圖控制器:當屏幕上出現警告時,我該如何暫停應用程序?

- (void)viewDidLoad 
{NSLog(@"viewDidLoad"); 
[super viewDidLoad]; 

[self generate]; 

[self.answer becomeFirstResponder]; 

UIAlertView *alert; 
{alert = [[UIAlertView alloc] 
      initWithTitle:@"Welcome to iCanMultiply!" 
      message:@"Tap 'Start' to start the game" 
      delegate: self 
      cancelButtonTitle:@"Start" 
      otherButtonTitles: nil]; 
} 

[alert show]; 
[alert release]; 

alertTimer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(showAlert) userInfo:nil repeats:YES]; 
} 

-(void)showAlert { 
    UIAlertView *alert = [[UIAlertView alloc] 
         initWithTitle:@"Game Over" 
         message:@"You ran out of time" 
         delegate:self 
         cancelButtonTitle:@"Try Again" 
         otherButtonTitles: nil]; 

[alert show]; 
[alert release]; 
} 

- (IBAction)submit:(id)sender { 
int num = [answer.text intValue]; 


UIAlertView *alert; 
if (num == label.tag) 
{ 
    NSUInteger messageCount = [messagesCorrect count]; 
    int randomMessageIndex = arc4random() % messageCount; 

    alert = [[UIAlertView alloc] 
      initWithTitle:@"Correct!" 
      message:[messagesCorrect objectAtIndex:randomMessageIndex] 
      delegate:self 
      cancelButtonTitle:@"Next Question" 
      otherButtonTitles: nil]; 

    alert.tag = 1; 

} else 
{ 
    NSUInteger messageCount = [messagesIncorrect count]; 
    int randomMessageIndex = arc4random() % messageCount; 

    alert = [[UIAlertView alloc] 
      initWithTitle:@"Wrong!" 
      message:[messagesIncorrect objectAtIndex:randomMessageIndex] 
      delegate:self 
      cancelButtonTitle:@"Try Again" 
      otherButtonTitles: nil]; 
    } 

    [alert show]; 
    [alert release]; 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (alertView.tag == 1) 
    { 
     [self generate]; 

     answer.text = @""; 
    } 
} 

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { 

    } 

請注意,我的應用程序委託是一樣的是,當我第一次開始對應用程序的工作。

+0

是你做了,謝謝你。 –

回答

0

對不起,我沒有看過你的代碼,但總的來說,這是你如何暫停NSTimer。

您設置了伊娃(在你的.h):

NSTimer *theTimer; 

當你想要啓動它:

if (!theTimer) { 
    theTimer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(showAlert) userInfo:nil repeats:YES]; 
} 

當你想停止它

if (theTimer) { 
    [theTimer invalidate]; 
    theTimer = nil; 
} 
+0

我在停止它之後會再次啓動計時器,因爲您發佈的開始只能在開始時啓動它 –

+0

您做同樣的事情 - 當您停止它時將其設置爲零,並且當您想要時再次創建它它開始射擊 –

相關問題