Я хочу показать пользователю, сколько времени ему или ей потребовалось ответить на определенное количество вопросов (например, 5) в качестве предупреждения. Я считаю, что у меня установлен правильный таймер, хотя я могу ошибаться. Как бы я закончил кодирование?
- (void)updateCounter:(NSTimer *)theTimer {
static int count = 0;
count += 1;
NSString *s = [[NSString alloc]
initWithFormat:@"%d", count];
self.timer.text = s;
[s release];
}
- (void)generate
{
int a = 1 + arc4random() % 12;
int b = 1 + arc4random() % 12;
int sum = a * b;
label.text = [NSString stringWithFormat: @"%d * %d = ", a, b];
label.tag = sum;
}
- (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];
[NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:@selector(updateCounter:)
userInfo:nil
repeats:YES];
}
- (IBAction)submit:(id)sender {
int num = [answer.text intValue];
UIAlertView *alert;
if (num == label.tag)
{
// answer was correct
alert = [[UIAlertView alloc]
initWithTitle:@"Correct"
message:@"Bet you can't do that twice!"
delegate:self
cancelButtonTitle:@"Next Question"
otherButtonTitles: nil];
// use the alert tag to mark that answer was correct
alert.tag = 1;
} else
{
// answer is incorrect
alert = [[UIAlertView alloc]
initWithTitle:@"Wrong!"
message:@"Sorry, try again..."
delegate:self
cancelButtonTitle:@"Try Again"
otherButtonTitles: nil];
}
// show and release the alert
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 1)
{
[self generate];
answer.text = @"";
}
}