Adding Outlets in Xcode 4
I'm creating IBOutlets by dragging a UI component from within the XIB editor into my view controller's header file. I'm dragging and dropping within the curly braces so that they are simply treated as instance variables and not properties. However, in spite of this I noticed release statements in my viewDidUnload and dealloc methods. Isn't this unnecessary? Like I said, these are not proper开发者_如何学Cties I'm creating. Why is the code being generated to release these objects when there is no retain, alloc, copy etc ?
On iOS, if an outlet is declared to be an ivar instead of a property, you are responsible for releasing it at the appropriate times (i.e. in -dealloc
and in -viewDidUnload
. You should also set them to nil
in the latter.)
This is due to a quirk of memory management on iOS. Regardless, my personal preference is to avoid using readwrite
properties for outlets, because IMHO it breaks the view controller's encapsulation in a bad way.
I think the reason is that all of your outlets should be retained properties. That way all of the views subviews (your buttons etc.) will be guaranteed to live longer than the view itself. I have never heard of coding in the way you have. Usually you just type the code and connect your outlets in the .xib.
You don't need to make them properties, Objective-C before 2.0 didn't include properties, properties are just syntactic sugar.
they are retained when they are pulled out of the nib.
check out The Nib Object Life Cycle section
精彩评论