我想在屏幕上显示警报时暂停屏幕,然后在警报解除时撤消暂停。我该怎么办? 这是我的视图控制器:
- (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 {
}
请注意,我的App Delegate与我刚开始使用该应用程序时的相同。
答案 0 :(得分:0)
抱歉,我没有看过你的代码,但一般来说这就是你暂停NSTimer的方式。
你设置了一个ivar(在你的.h中):
NSTimer *theTimer;
当你想要开始时:
if (!theTimer) {
theTimer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(showAlert) userInfo:nil repeats:YES];
}
当你想要阻止它时
if (theTimer) {
[theTimer invalidate];
theTimer = nil;
}