Template09
Template09

Reputation: 164

How to release subview in iOS

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.

Upvotes: 0

Views: 181

Answers (4)

chandan
chandan

Reputation: 2453

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

Upvotes: 2

chandan
chandan

Reputation: 2453

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

Upvotes: 0

Dharmbir Singh
Dharmbir Singh

Reputation: 17535

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

Upvotes: 0

Balu
Balu

Reputation: 8460

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

Upvotes: 0

Related Questions