poyo fever.
poyo fever.

Reputation: 752

Creating views programatically instead of using interface builder

when creating a View programatically I have to refrence the subviews as strong because I instanciate them and when I add them to the Container, the container refrence them also as strong

I'd like to know if it will not create a reference cycle which will leave a memory leak, and if yes, what will be the best approach

EDIT :

for example i have a class named InterfaceBuilder who in this case contains a function who build loading view :

-(UIView*) buildLoadingView :(SuiviCommercialViewController*) viewController
{
    UIView *view = [[UIView alloc] initWithFrame:viewController.view.frame];

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0.1, 0, view.bounds.size.width, 50)];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.1, 0, view.bounds.size.width, 50)];
    [imageView setImage: [UIImage imageNamed:@"ToolBar_white_1"]];
    [headerView addSubview:imageView];

    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake((view.bounds.size.width/2)-100, 15, 200, 20)];
    title.backgroundColor = [UIColor clearColor];
    title.text = [NSString stringWithFormat:@"Mon suivi propriétaires(s)"];
    title.font = [UIFont fontWithName:@"Helvetica-Bold" size:16];
    [title sizeToFit];
    [headerView addSubview:title];

    viewController.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    viewController.activityIndicator.frame=CGRectMake(view.bounds.size.width/2, view.bounds.size.height/2, 20, 20);
    viewController.activityIndicator.color = [UIColor blackColor];
    [view addSubview:viewController.activityIndicator];

    [view addSubview:headerView];

    return view;

}

Upvotes: 1

Views: 90

Answers (2)

Gavin
Gavin

Reputation: 8200

What you're describing shouldn't create circular references, though it's impossible to determine for sure without seeing your exact code. But if two objects are referencing the same set of objects, it simply means that it will (possibly) take those objects slightly longer to ever get deallocated, because they're waiting on two different other objects to eventually get deallocated.

Now if any of those subviews that you're referencing as strong have references back to either the container view or the other object that's got strong references to them, then you'll have a circular reference and the memory will never get freed unless you explicitly break it by setting the references to nil. You could also have your references to the subviews be weak, which would reduce the chances of a retain cycle.

If you're making them strong only because they're getting deallocated before you add them as subviews, you can use a local variable that will keep a strong reference until you can add it as a subview, like this:

UIView *subview = [[UIView alloc] init]; // subview keeps a strong reference while in the scope of the method
self.firstSubview = subview; // property firstSubview is a weak property
[self.containerView addSubview:subview]; // The container view now has a strong reference to subview, so when the local variable goes out of scope, it won't get deallocated

Upvotes: 1

aethe
aethe

Reputation: 338

There will be no reference cycle.

The reference cycle happens, when a Superview has a strong reference to its Subview, and that Subview has a strong reference to the Superview.

Upvotes: 1

Related Questions