0

我正在尝试分配和初始化作为 NSObject (Circle) 一部分的 UIImageViews,并且我有一个由这些 Circles 组成的数组。但是,XCode 不喜欢我分配和初始化 UIImageViews。

这是代码:

//allocate and set circleArray
circleArray = [[NSMutableArray alloc] init];
for (int i = 0; i < 16; i++) {
    [circleArray addObject:[Circle alloc]];
    [[circleArray objectAtIndex:i] setTouched:0];
    [[circleArray objectAtIndex:i] imageView] = [[UIImageView alloc] init]; //Problem Here: "Expression is not assignable"
}

//some code setting up the columns and beats shown below

for (int i = 0; i < 16; i++) {
    [[[circleArray objectAtIndex:i] imageView] setFrame:CGRectMake([[circleArray objectAtIndex:i] column]*80-75, [[circleArray objectAtIndex:i] beat]*80+5, 70, 70)];

    [self.view addSubview:[[circleArray objectAtIndex:i] imageView]];

    if ([[circleArray objectAtIndex:i] beatMod] == 0) {
        [[[circleArray objectAtIndex:i] imageView] setImage:redCircle];
    }
    else if ([[circleArray objectAtIndex:i] beatMod] == 0.5) {
        [[[circleArray objectAtIndex:i] imageView] setImage:blueCircle];
    }
    else if ([[circleArray objectAtIndex:i] beatMod] == 0.25 || [[circleArray objectAtIndex:i] beatMod] == 0.75) {
        [[[circleArray objectAtIndex:i] imageView] setImage:yellowCircle];
    }
    else {
        [[[circleArray objectAtIndex:i] imageView] setImage:greenCircle];
    }
}

如果我取出有错误的行,则屏幕上不会显示任何内容。我应该分配和初始化 UIImageViews 以使其显示,还是在分配和初始化 NSObject 时已经为我完成?我该如何解决?

4

1 回答 1

0
  1. 您不需要for 循环第一行中init的对象吗?Circle

  2. 语法对我来说有点奇怪。导致您出现问题的行,它的 LHS 正在返回一个对象。你不应该它设置成什么。你可能应该使用 cast like

    ((Circle *)[circleArray objectAtIndex:i]).imageView = [[UIImageView alloc] init];

  3. 您可能需要Circle在将其添加到阵列之前进行设置。所以就

    Circle *c = [[Circle alloc] init];

    c.imageView = blablabla;

    [array addObject:c];

或类似的东西。

(嗯..不知道为什么块代码不起作用..)

于 2012-08-03T21:58:08.377 回答