Subclassed UIView没有显示添加到其中的UIImageViews

时间:2012-11-14 03:21:38

标签: iphone ios uiview uiimageview addsubview

所以我有一个名为CSImageViews(uiview的子类)的类,它本质上是一行UIImageViews,包含在一个子类UIView中。

我添加了一个像这样的自定义init方法(node / yaml包含uiimage名称数据):

- (id)initWithFrame:(CGRect)frame withNode:(NSDictionary *)node
{
    self = [super initWithFrame:frame];
    if (self) {
        _node = node;
        _imageViewYAML = [node objectForKey:@"items"];
        _imageViews = [self getImageViewsForItems:_imageViewYAML];
    }
    return self;
}

我的getImageViewsforItems就是这样(将它们全部添加到子视图中):

-(NSArray *)getImageViewsForItems:(NSArray *)items
{
    NSMutableArray *ivs = [NSMutableArray arrayWithCapacity:[items count]];
    for (int i = 0; i < [items count]; i++) {
        NSDictionary *item = [items objectAtIndex:i];
        NSString *type = [item objectForKey:@"type"];
        NSString *name = [item objectForKey:@"name"];
        UIImage *image = [UIImage imageNamed:name];
        UIImageView *iv = [[UIImageView alloc] init];
        iv.image = image;
        iv.tag = i;
        [ivs addObject:iv];
        [self addSubview:iv];
    }
    return ivs;
}

现在,当我将这个自定义视图添加到我的主视图时,没有任何反应:

CSImageViews *imageViews = [[CSImageViews alloc] initWithFrame:frame withNode:node];
[view addSubview:imageViews];

但如果我首先将这个'csimageviews'容器添加到新的uiview中,它会出现:

CSImageViews *imageViews = [[CSImageViews alloc] initWithFrame:frame withNode:node];
UIView *v = [[UIView alloc] initWithFrame:frame];
[v addSubview:imageViews];
[view addSubview:v];

想法?

1 个答案:

答案 0 :(得分:0)

我的愚蠢错误。在这个特定的子类UIView中,我有一个方法,每次显示其父视图控制器时都会调用它。这个方法实际上从视图中删除了所有子视图...所以当我在初始视图中渲染时,我无法看到uiimageviews ... doh。