Zane Helton
Zane Helton

Reputation: 1052

Memory Management For User Interface

I'm working on an app's user interface, and I'm doing it programmatically. I understand the idea behind retain cycles, and feel quite confident I could identify one, but Xcode is giving me warnings when I'm trying to avoid a retain cycle.

Essentially, I'm creating a property called titleLabel which will have my title, and in viewDidLoad I'm initializing it, and placing it on my view. The only problem is, I'm getting an Xcode warning that says:

Assigning retained object to weak variable; object will be released after assignment

My property definition is as follows:

@property (nonatomic, weak) UILabel *titleLabel;

Obviously I could fix the Xcode warning by changing weak to strong in my property definition, but I believe that would create a retain cycle (from my understanding) because the button is holding onto the View Controller and the View Controller is holding onto the button.

Can anyone give me some insight as to how to do this properly?

Upvotes: 0

Views: 49

Answers (3)

TheAppMentor
TheAppMentor

Reputation: 1099

When you create a UI element and add it as a subview to parent view. The Parent view keeps a strong reference to sub View. In your case, you could just create a UILabel variable within your function and add it to the parent.

You could then declare a weak property that keeps a reference to this newly created label. This way, the parent view "owns" the label, and it takes care of cleaning up the label when the parent view goes off screen.

@interface MasterViewController ()

@property (nonatomic,weak) UILabel * theLabel;

@end

@implementation MasterViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UILabel* myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    [self.view addSubview:myLabel];
    self.theLabel = myLabel;
}

Upvotes: 0

Avi
Avi

Reputation: 7552

Your understanding is flawed. If the label were to retain a reference to its view controller, you would have a retain cycle if the property were defined as strong. However, views do not retain references to their view controllers, so there is no cycle. Xcode is warning you correctly that your label reference will go away after the assignment. You aren't even saved by adding it as a subview (which would retain it), because it's already released by the time you try.

The one quasi-exception to views not retaining references to controllers is with table and collection views, which have delegates and datasources which are usually their managing view controller. However, those delegate/datasource properties are defined as assign properties (which has another set of problems) so there is no retain cycle there, either.

Upvotes: 1

Divyanshu Sharma
Divyanshu Sharma

Reputation: 559

i think you can set property to strong which will retain its memory and when your view will disappear call then you can set this object to nil which will release its memory.

Upvotes: 0

Related Questions