As a rule, I usually create accessors for IBOutlet
s.
In ARC or non-ARC projects, I usually do the following:
//.h (ARC) @property (nonatomic, weak) IBOutlet UILabel* myLabel; //.h (non-ARC) @property (nonatomic, retain) IBOutlet UILabel* myLabel; //.m @synthesize myLabel;
That way you can let the compiler create an instance variable for you. But you can also declare your instance variable and tell the compiler about it.
Then you can use the accessors / instance variable wherever you want.
The Apple Memory Management Guide says that you need to avoid access methods in the init
or dealloc
methods when you have non-ARC projects. So for example:
This is very important in projects other than ARC. The reason is that if there is no access, KVC will assign the nib object to the instance variable and put a save on it. If you forget to release it, you may have a memory leak. Using an accessory forces you to free this object at the end.
I highly recommend reading friday-qa-2012-04-13-13-nib-memory-management by Mike Ash. This is a very cool article on knife and memory management.
Hope this helps.
source share