我使用以下代码选择正确答案,当它达到限制时,它会显示结果视图,
-(void)displayImageAndButton:(int)pos
{
CATransition *transition = [CATransition animation];
transition.duration = 1.0f;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionFade;
[animage.layer addAnimation:transition forKey:nil];
animage.image=[UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg",[imageary objectAtIndex:pos]]];
//NSLog(@"pos value%d",pos);
UIView *newView = [self.view viewWithTag:1];
if([newView isKindOfClass:[UIButton class]])
{
NSLog(@"pos value%d",pos);
UIButton *newButton = (UIButton *)newView;
name=[imageary objectAtIndex:pos++];
[newButton addTarget:self action:@selector(submitGuess:)
forControlEvents:UIControlEventValueChanged];
[newButton setTitle:name forState:UIControlStateNormal];
}
newView = [self.view viewWithTag:2];
if([newView isKindOfClass:[UIButton class]])
{
NSLog(@"pos value%d",pos);
UIButton *newButton = (UIButton *)newView;
name=[imageary objectAtIndex:pos++];
[newButton addTarget:self action:@selector(submitGuess:)
forControlEvents:UIControlEventValueChanged];
[newButton setTitle:name forState:UIControlStateNormal];
}
newView = [self.view viewWithTag:3];
if([newView isKindOfClass:[UIButton class]])
{
NSLog(@"pos value%d",pos);
UIButton *newButton = (UIButton *)newView;
name=[imageary objectAtIndex:pos++];
[newButton addTarget:self action:@selector(submitGuess:)
forControlEvents:UIControlEventValueChanged];
[newButton setTitle:name forState:UIControlStateNormal];
}
newView = [self.view viewWithTag:4];
if([newView isKindOfClass:[UIButton class]])
{
NSLog(@"pos value%d",pos);
UIButton *newButton = (UIButton *)newView;
name=[imageary objectAtIndex:pos++];// Here the problem coming when it value 13 its getting error what should i change
[newButton addTarget:self action:@selector(submitGuess:)
forControlEvents:UIControlEventValueChanged];
[newButton setTitle:name forState:UIControlStateNormal];
}
}
更新^^^
- (IBAction)submitGuess:(id)sender
{
UIButton *mybutton = (UIButton *)sender;
if ([mybutton.titleLabel.text isEqualToString:[imageary objectAtIndex:positionOfQuest]])
{
//titles equal
[self alertshow];
positionOfQuest++;
totalGuesses++;
timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(removeImageView) userInfo:nil repeats:NO];
[self displayImageAndButton:positionOfQuest];
[lb1 setText:[NSString stringWithFormat:@"Question %i ", positionOfQuest + 1]];
} else {
totalGuesses++;
[self alertshow2];
}
NSLog(@"toatguess value%d",positionOfQuest);
NSLog(@"toatguess value%d",numCorrect);
if(positionOfQuest==numCorrect)
{
[self resulmsg];
[lb3 setText:[NSString stringWithFormat: @"%i guesses, %.02f%% correct", totalGuesses,
1000 / (float)totalGuesses]];
[lb4 setText:[NSString stringWithFormat: @"%.02f%%",1000 / (float)totalGuesses]];
NSLog(@"lb4%@",lb4);
}
}
我有10个限制,即。numcorrect 值为 10。但当它达到 9 时,它显示错误。
2012-11-28 15:27:56.430 Kidsapp[4225:c07] toatguess value8
2012-11-28 15:27:56.431 Kidsapp[4225:c07] toatguess value10
2012-11-28 15:27:57.409 Kidsapp[4225:c07] toatguess value9
2012-11-28 15:27:57.409 Kidsapp[4225:c07] toatguess value10
2012-11-28 15:27:58.017 Kidsapp[4225:c07] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 13 beyond bounds [0 .. 12]'
这里有什么错误?