1

在此处输入图像描述大家好,实际上我有很多 UIView 自定义类对象作为 UIScrollView 内的 UIViewController 类视图的子视图。我想检查 UIView 自定义类对象的颜色。我使用的代码如下: -

    - (void) RectColorCheck:(id)sender
{
    NSArray *subViews = [[NSArray alloc] init];
    subViews = [self.scrollView subviews];
    NSLog(@"array----%@",subViews);

        for (viewCG in subViews) 
        {
            if ([viewCG.backgroundColor isEqual: [UIColor darkGrayColor]])
            {
                [viewCG setBackgroundColor:[UIColor orangeColor]];
            }
        }
}

但它不起作用 viewCG 的子视图数组为空。下面的代码在 viewCG 子视图中添加了自定义类(UIView)对象:-

for (int i=0; i<[mapDataArr count]; i++)
    {
        X = [[[mapDataArr objectAtIndex:i] valueForKey:@"X"] intValue];
        Y = [[[mapDataArr objectAtIndex:i] valueForKey:@"Y"] intValue];
        W = [[[mapDataArr objectAtIndex:i] valueForKey:@"W"] intValue];
        H = [[[mapDataArr objectAtIndex:i] valueForKey:@"H"] intValue];

        circleVwObj = [[CircleView alloc] init];
        circleVwObj.frame = CGRectMake(X,Y, W, H);
        circleVwObj.tag = i;

        circleVwObj.lbl.frame = CGRectMake(2,2, circleVwObj.frame.size.width, circleVwObj.frame.size.height/2);
        circleVwObj.lbl.text = [[mapDataArr objectAtIndex:i] valueForKey:@"standId"];
        NSLog(@"lbl text---%@", circleVwObj.lbl.text);
        circleVwObj.lbl.font = [UIFont boldSystemFontOfSize:11];
        circleVwObj.lbl.backgroundColor = [UIColor clearColor];
        circleVwObj.lbl.textColor = [UIColor whiteColor];
        circleVwObj.lbl.textAlignment = UITextAlignmentCenter;
        circleVwObj.lbl.minimumFontSize = 11;
        circleVwObj.lbl.adjustsFontSizeToFitWidth = YES;
        circleVwObj.lbl.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);

        [self.viewCG addSubview:circleVwObj];
    }
4

1 回答 1

1

如果subviews数组是nil,则很可能self.viewCG也是nil如此并且尚未初始化。

此外,您应该使用isEqual:来比较颜色。运算符只是比较指针标识(在这种==特殊情况下,这实际上可能会给您预期的结果,但它可能会中断)。

于 2012-11-05T09:54:03.947 回答