objective-c - 查找 UIView 对象的颜色并更改颜色

标签 objective-c ios xcode xcode4.5 ios5

enter image description here大家好,实际上我有很多 UIView 自定义类对象作为 UIViewController 类 View 的 subview ,它位于 UIScrollView 中。我想检查 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 的 subview 数组为空。 下面的代码在 viewCG subview 中添加了自定义类 (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];
    }

最佳答案

如果subviews数组是nil,那么self.viewCG很可能也是nil并且还没有被初始化。

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

关于objective-c - 查找 UIView 对象的颜色并更改颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13229514/

相关文章:

ios - iOS 7.1 SDK 中的内联日期选择器

ios - 在 Parse 数据浏览器中隐藏字符串值

ios - 使 UITabBarItem 项目之一在 UITabBarController 中没有 View

swift - 选择文本字段时如何使键盘不出现?

Objective-C 代码 Swift 框架内部

ios - 在日期之间获取(实际日期,而不是天数)

iphone - 使用view.bounds进行边界

iOS 设置屏幕 UIAlertView(最好在按下 'back' 时)

iphone - 我们如何知道 map 坐标是否在当前区域?

c++ - 在 xcode C++ 项目中编译为 C99 文件