0

这是添加子视图的代码,我想在添加子视图之前检查它。

numberOfViews = [[GlobalVariable sharedInstance].itemNewsDetail count];


for (int i = 0; i < numberOfViews; i++) {
    @try{
        CGFloat xOrigin = i * 320;
        CGRect frame;
        frame.size = CGSizeMake(320, 365);
        frame.origin.x = xOrigin;
        frame.origin.y = 0;

        detailVC = [[DetailScrollVC alloc]initWithNibName:@"DetailScrollVC" bundle:nil];
        detailVC.view.frame = frame;
        [detailVC loadViewByIndex:i];

        UIFont *font = detailVC.txtBodyNews.font;
        detailVC.txtBodyNews.font = [font fontWithSize:currentFontSize];
        detailVC.txtBodyNews.tag = i;

        [scrollDetail addSubview:detailVC.view];
        [scrollDetail sizeToFit];
        [detailVC.view release];
    }@catch (NSException *exception) {
        NSLog(@"ERROR HANDLING : %@",exception);
    }
}

如何检查和释放我之前已经添加的所有子视图。

谢谢。

4

4 回答 4

2

如果您想从滚动视图或任何其他视图中删除子视图,请尝试以下代码:

 numberOfViews = [[GlobalVariable sharedInstance].itemNewsDetail count];

 for (UIView *subviewElement in scrollDetail.subviews) 
 {
     [subviewElement removeFromSuperview];
 }

 for (int i = 0; i < numberOfViews; i++)
 {
     @try
     {
        CGFloat xOrigin = i * 320;
        CGRect frame;
        frame.size = CGSizeMake(320, 365);
        frame.origin.x = xOrigin;
        frame.origin.y = 0;

        detailVC = [[DetailScrollVC alloc]initWithNibName:@"DetailScrollVC" bundle:nil];
        detailVC.view.frame = frame;
        [detailVC loadViewByIndex:i];

        UIFont *font = detailVC.txtBodyNews.font;
        detailVC.txtBodyNews.font = [font fontWithSize:currentFontSize];
        detailVC.txtBodyNews.tag = i;

        [scrollDetail addSubview:detailVC.view];
        [scrollDetail sizeToFit];
        [detailVC release];
     }
     @catch (NSException *exception) {
        NSLog(@"ERROR HANDLING : %@",exception);
     }
 }

我希望它可以帮助您更好地理解。谢谢

于 2013-05-10T06:54:42.023 回答
0

试试这样

for (UIView *vie in self.view.subviews)
    {
        if([vie isKindOfClass:[UIImage class]])//here place your object class name for removeing that particuler subview from superview.
            [vie removeFromSuperview];
    }
于 2013-05-10T04:48:32.917 回答
0

如果您在谷歌上没有找到您的解决方案,请先尝试在谷歌上搜索您的问题,在这种情况下,您应该发布您的问题。在下面的链接中查看您的答案

  1. 如何遍历 UIView 的所有子视图及其子视图及其子视图

  2. 如何列出iOS中uiviewcontroller中的所有子视图?

  3. http://iphonedevsdk.com/forum/iphone-sdk-development/5599-removing-all-subviews-from-a-view.html

于 2013-05-10T04:55:20.607 回答
0

如果您想从滚动视图或任何其他视图中删除子视图,请尝试以下代码:

NSArray *viewsToRemove = [yourView subviews];

for (UIView *subviewElement in viewsToRemove) 
{
    [subviewElement removeFromSuperview];
}

注意:- yourView 是类似 scrollDetail 或 self.view 等的任何东西。

我希望它可以帮助您更好地理解。谢谢

于 2013-05-10T05:01:10.897 回答