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 から始まるため、最大インデックス値は [[配列カウント]-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 に答える