0

This is the code that add subview and I want to check it before adding subview.

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);
    }
}

how to check and release all subviews that I already add before.

Thanks.

DD_
  • 7,230
  • 11
  • 38
  • 59
Template09
  • 164
  • 2
  • 10

4 Answers4

2

If you want to remove subviews from your scrollview or any other view than try this code:

 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);
     }
 }

I hope it helps you for better understanding . Thanks

chandan
  • 2,453
  • 23
  • 31
  • thanks for your help @chandan.. But my apps still force close with another error.. lldb. what do you think about that? – Template09 May 10 '13 at 07:18
0

try like this,

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];
    }
Balu
  • 8,470
  • 2
  • 24
  • 41
0

Please try to search first your question on google if you not found your solution on google in that case you should post your question. See your answer on below link

  1. How can I loop through all subviews of a UIView, and their subviews and their subviews

  2. How to list out all the subviews in a uiviewcontroller in iOS?

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

Community
  • 1
  • 1
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
0

If you want to remove subviews from your scrollview or any other view than try this code:

NSArray *viewsToRemove = [yourView subviews];

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

Note :- yourView is any thing like scrollDetail or self.view etc.

I hope it helps you for better understanding . Thanks

chandan
  • 2,453
  • 23
  • 31
  • But I still get received memory warning issue in this function.. What do you think about that @chandan?? – Template09 May 10 '13 at 06:14
  • In my apps, I built a tableview that while select action, I put a pagination addsubview like code that I have given. I think the memory warning issue is when I generate that addsubview and I just addsubview it again and replace that addSubview. – Template09 May 10 '13 at 06:36
  • I have apply it at my end and it's working fine. I have added another answer. I have merged my answer with your code. Please let me know if any problem still occurring. – chandan May 10 '13 at 06:56
  • Warning might be [detailVC.view release]; so change it [detailVC release]; and check once that you set delegate of your scrollview or not. Some time it show warning. And if still it create problem than assign property to your scrollDetail and use it as self.scrollDetail. Thanks – chandan May 10 '13 at 07:13
  • I can't change it like you have said, if I change to [detailVC release], my apps get lldb error. – Template09 May 10 '13 at 07:29
  • you can use if([subviewElement isKindOfClass:[DetailScrollVC class]]){} . isKindOfClass method check that what kind of subviews available inside your view. In place of DetailScrollVC you can put any view that you want to check. – chandan May 10 '13 at 07:58