在其他方法访问时,NSMutableArray崩溃

时间:2014-10-01 10:41:34

标签: ios iphone arrays xcode nsmutablearray

-(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];   
}

1 个答案:

答案 0 :(得分:1)

问题是ColorArray需要是类的实例变量(或@property),以便它在方法调用的

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

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

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

Color似乎已经是一个实例变量(或@property),所以这个概念对你来说不应该是陌生的。

附注:变量通常以小写字母开头并使用camal-case命名。