0

我使用以下代码选择正确答案,当它达到限制时,它会显示结果视图,

-(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]'

这里有什么错误?

4

3 回答 3

1

好吧,至于您的错误,很明显您正在尝试访问 12 数组的第 13 个对象。尝试放置一些断点来调试您的代码。

于 2012-11-28T10:17:16.920 回答
1

数组从索引 0 开始,因此您的最大索引值必须是 [[array count]-1]。根据您的代码 positionOfQuest 正在获取值 13 您收到错误

因为您的数组计数为 13,但数组的索引为 12 中的 0。

于 2012-11-28T12:34:40.303 回答
0

这是因为您positionOfQuest的价值为 13 并且您正在搜索

[imageary objectAtIndex:12];  

但是图像有对象直到索引 12(总共 13 个对象索引 0....12)

因此,根据您的问题,只需设置一个 if 条件

if ([imageary count] > positionOfQuest)
{
    if ([mybutton.titleLabel.text isEqualToString:[imageary objectAtIndex:positionOfQuest]])
    {
        //Your work
    }

}
于 2012-11-28T11:31:58.337 回答