Search code examples
iphoneiosmemory-managementdealloc

How to release IBOutlet defined as property?


sorry for this question, but I searched it and I didn't find an answer for that case.

I'm studying memory management for iOS and I understood, or I think so, the view lifecycle. But now I have a question on a IBOutlet (tat is linked to a UIImageView in my xib file). I have a class like this:

@interface MyClass : UIViewController 

@property (nonatomic, retain) IBOutlet UIImageView *myImage;

The question is: how can I release myImage? Is this ok?

- (void)dealloc {
    self.myImage = nil;
    [super dealloc];
}

- (void)viewDidUnload {
    [super viewDidUnload];
    self.myImage = nil;
}

Can someone explain why can't I call the release method on myView (if you had some lik it is good too!)?

Thanks in advance!


Solution

  • In general, you don't call release on a property, you would call it on the corresponding ivar. This is my standard way to handle IBOutlet properties:

    @interface MyClass
    
    @property (nonatomic, retain) IBOutlet UIImageView *myImageView;
    @property (nonatomic, retain) IBOutlet UILabel *myLabel;
    
    @end
    
    
    @implementation MyClass
    
    @synthesize myImageView = _myImageView;
    @synthesize myLabel = _myLabel;
    
    
    - (void)dealloc {
    
        [_myImageView release];
        [_myLabel release];
    
        [super dealloc];
    }
    
    @end