1

I am using XCode 5.1.1, targeting iOS 7.0.

When creating outlets from my storyboard using the Assistant editor. I notice I have a few choices to create properties or ivars. The one I have been using is dragging directly to my *.m @implementation and it creates code like:

@implementation AudioViewController
{
    __weak IBOutlet UILabel *posLabel;
    __weak IBOutlet UILabel *durationLabel;
    __weak IBOutlet UIButton *playButton;
}

I have no need to access these outside of this class, so this seems convenient, but I am wondering if there are any "gotchas" to this method vs creating properties, especially in regards to memory management. I read on other stack answers that you must create (weak) properties or I will have to [release] manually. I am wondering if this __weak takes care of that in this context?

Thanks!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Michael
  • 435
  • 4
  • 9
  • There's a great deal of discussion here [Getting my head around the practical applications of strong and weak pointers in Objective-C](http://stackoverflow.com/questions/13623884/getting-my-head-around-the-practical-applications-of-strong-and-weak-pointers-in) that should answer your question. (no you don't need to clear or release them) – David Berry Apr 29 '14 at 16:30
  • Thank you. I had read a number of these threads, but wanted to be sure in context of IBOutlets that there were no other pitfalls I may be unaware of since most threads seem to make them as properties instead of ivars and other threads mention that you would have to release ivars. There seems to be some contradictory answers here. – Michael Apr 29 '14 at 17:06
  • 1
    There are some pitfalls to be careful of, the biggest of them with IBOutput is probably that if you have a UILabel, for instance, which you remove from the view hierarchy for any reason, then it will be released and your out set to nil. So if you have a view you conditionally show and hide, you should make it strong, not weak. – David Berry Apr 29 '14 at 17:09
  • So whether or not it's a property or ivar is irrelevant then? – Michael Apr 29 '14 at 17:15
  • 1
    For purposes of this discussion, yeah, it's pretty irrelevant. – David Berry Apr 29 '14 at 18:38

1 Answers1

1

Creating properties and instance variables with the same modifier is mostly analogous. When you are using ARC, you do not have to release strong properties or instance variables - they will be released when the object is deallocated. Interface element outlets are usually created as weak, because they are retained by the view hierarchy. You should be careful; if you intend to remove the elements from the view hierarchy at some point, you should change the modifier to strong to ensure they are retained by the view controller. Top-level outlets should also be created as strong to make sure they are retained after nib load.

Léo Natan
  • 56,823
  • 9
  • 150
  • 195