-(void)Aray
{
    NSMutableArray *ColorArray = [[NSMutableArray alloc] init];
    if(Counter < NewColor)
    {
        [ColorArray addObject:[NSNumber numberWithInteger:ColorTemp]];

        Counter += 1;
    }
}

-(IBAction)Go:(id)sender
{
    NSMutableArray *ColorArray = [[NSMutableArray alloc] init];

    Color = [[ColorArray objectAtIndex:Index] intValue];

    if(Color == 2)
    {

        ColorLabel.text = @"The Color is Black";
        Screen.image = [UIImage imageNamed:@"BlackTile.png"];
    }
    else
    {
        Screen.image = [UIImage imageNamed:@"Tunnel.png"];
        ColorLabel.text = @"The Color is Green";
    }
    Index += 1;
}

-(IBAction)Black:(id)sender
{
    ColorTemp = 2;
    NewColor += 1;
    [self Array];
}

-(IBAction)Green:(id)sender
{
    ColorTemp = 1;
    NewColor += 1;
   [self Array];
}

最佳答案

问题是ColorArray必须是该类的实例变量(或@property),以便它在方法调用之外持久存在。

无论Index的值如何,此代码始终将崩溃:

NSMutableArray *ColorArray = [[NSMutableArray alloc] init];

Color = [[ColorArray objectAtIndex:Index] intValue];


Color似乎已经是一个实例变量(或@property),因此此概念不应与您无关。

旁注:变量通常以小写字母开头,并使用小写字母命名。

10-07 17:52