在NSObjects数组中分配和初始化UIImageViews

时间:2012-08-03 21:31:08

标签: objective-c xcode

我正在尝试分配和初始化属于NSObject(Circle)的UIImageViews,我有一个这些圈子的数组。但是,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时是否已经为我完成了? 我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

  1. 您是否需要在for循环的第一行中init Circle个对象?

  2. 语法对我来说有点奇怪。导致您出问题的行,其LHS 返回对象。你不应该设置它。您应该使用类似

    的演员表

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

  3. 您可能需要先设置Circle,然后再将其添加到阵列中。所以只是

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

    c.imageView = blablabla;

    [array addObject:c];

  4. 或类似的东西。

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