热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

HelloGuysactuallyihavealotofUIViewcustomclassobjectsasasubviewofaUIViewControllerc

enter image description hereHello Guys actually i have a lot of UIView custom class objects as a subview of a UIViewController class view which is inside a UIScrollView. And i want to check colors of UIView custom class objects. code i use is given below:-

Hello Guys实际上我有很多UIView自定义类对象作为UIVcrollView内的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]];
            }
        }
}

but it not works the subview array of viewCG is null. And the below code which adds the custom class(UIView) objects in viewCG subviews:-

但它不起作用viewCG的子视图数组为null。以下代码在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];
    }

1 个解决方案

#1


1  

If the subviews array is nil, it is very likely that self.viewCG is nil as well and hasn't been initialized.

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

Also, you should use isEqual: to compare colors. The == operator just compares pointer identity (which might actually give you the expected result in this particular case, but it's likely to break).

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


推荐阅读
author-avatar
董雪高
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有