次のコードを使用して正しい答えを選択し、制限に達すると結果ビューを表示します。
-(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]'
ここでのエラーは何ですか?