我想向用户显示他或她将一定数量的问题(例如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 = @"";
}
}
答案 0 :(得分:0)
存储该值,然后将其显示在警报视图中。
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message@"It took you %@ seconds.",time delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
让我知道时间是否有效。我还没有测试过,但它应该可以工作。