无法从视图中删除子视图?

时间:2013-04-24 11:21:35

标签: ios objective-c uiview

我正在尝试从UIView中删除指标子视图,但它会删除它们而不仅仅是子视图。

我的观点是:

 UIView * aView = [[UIView alloc] initWithFrame:CGRectMake(0.5*winSize.width-winSize.width/3, 0.85*winSize.height , winSize.width/1.5, winSize.width/8)];
    aView.backgroundColor = [UIColor clearColor];
    aView.tag  = 31000;
    UIActivityIndicatorView  *av = [[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
    av.frame=aView.bounds;
    [av setBackgroundColor:[UIColor colorWithRed:0.23 green:0.23 blue:0.23 alpha:0.85]];
    av.layer.cornerRadius=4;
    av.tag = 31001;
    [aView addSubview:av];
    [[[CCDirector sharedDirector]view]addSubview:aView];
    [av startAnimating];

我试图用我在这里找到的许多方法删除它:

 UIActivityIndicatorView *tmpimg = (UIActivityIndicatorView *)[[[CCDirector sharedDirector]view] viewWithTag:31001];
    if(tmpimg)
        [((UIView *)tmpimg) removeFromSuperview];

   UIView *removeView;
    while((removeView = [[[CCDirector sharedDirector]view] viewWithTag:31001]) != nil)
    {
        [removeView removeFromSuperview];
    }

 UIView * aView = [[[CCDirector sharedDirector]view] viewWithTag:31000];
    if(aView)
        for(UIView *subview in [aView subviews])
            [subview removeFromSuperview];

但它总是会删除它们......我迷失了......

1 个答案:

答案 0 :(得分:3)

这将从您的视图中删除任何UIActivityIndi​​catorView,而无需标记它是什么是一个糟糕的解决方案

for (UIView *aSubView in aView) {
    if ([aSubView isKindOfClass:[UIActivityIndicatorView class]])
        [aSubView removeFromSuperview];
}