Declaring IBOutlet inside or outside @interface?

Both of those are still "inside the interface" so your title it a bit confusing but I see what you are asking In many cases the result of either approach will be the same but they are different. An IBOutlet property will call the property's setter method which gives you an opportunity to override that setter if setting that property should have some side effect I prefer to use outlets on properties because I think it makes the memory management of the objects loaded from the nib much clearer. Take a look at memory management of nib objects and I think you will see what I mean Objects in the nib file are created with a retain count of 1 and then autoreleased.As it rebuilds the object hierarchy, UIKit reestablishes connections between the objects using setValue:forKey:, which uses the available setter method or retains the object by default if no setter method is available.

This means that (assuming you follow the pattern shown in “Outlets�) any object for which you have an outlet remains valid. If there are any top-level objects you do not store in outlets, however, you must retain either the array returned by the loadNibNamed:owner:options: method or the objects inside the array to prevent those objects from being released prematurely IBOutlet ivars will call setters for those ivars if they exists and directly retain the object loaded from the nib if no setter is found Advertising the property as the IBOutlet at least makes it clear that the property's setter will always be used and follow whatever memory management rule has been set for that property Finally I argue that IBOutlets are part of the public interface of a class and it is therefore better to expose methods (via a property) for working with them eager than using -setValue:forKey: to manipulate the backing ivars which should be an implementation detail.

Both of those are still "inside the interface" so your title it a bit confusing but I see what you are asking. In many cases the result of either approach will be the same but they are different. An IBOutlet property will call the property's setter method which gives you an opportunity to override that setter if setting that property should have some side effect.

I prefer to use outlets on properties because I think it makes the memory management of the objects loaded from the nib much clearer. Take a look at memory management of nib objects and I think you will see what I mean. Objects in the nib file are created with a retain count of 1 and then autoreleased.As it rebuilds the object hierarchy, UIKit reestablishes connections between the objects using setValue:forKey:, which uses the available setter method or retains the object by default if no setter method is available.

This means that (assuming you follow the pattern shown in “Outlets�) any object for which you have an outlet remains valid. If there are any top-level objects you do not store in outlets, however, you must retain either the array returned by the loadNibNamed:owner:options: method or the objects inside the array to prevent those objects from being released prematurely. IBOutlet ivars will call setters for those ivars if they exists and directly retain the object loaded from the nib if no setter is found.

Advertising the property as the IBOutlet at least makes it clear that the property's setter will always be used and follow whatever memory management rule has been set for that property. Finally I argue that IBOutlets are part of the public interface of a class and it is therefore better to expose methods (via a property) for working with them eager than using -setValue:forKey: to manipulate the backing ivars which should be an implementation detail.

Thanks Jonah, great explanation. :) – ludicco Jan 25 at 10:41.

The two styles are interchangeable, there is no difference in the generated code or the way objects will be loaded from a nib. Really. However, both styles have a redundant line.

Simply leave out the ivar declaration. Just the line @property (nonatomic, retain) IBOutlet CustomCell *customCell; is sufficient in the modern runtime. If you have a complex project, I suggest moving all the outlets out of the public interface into a separate header file.

Most outlets are private interface, the only reason to have them in a header is so Interface Builder can find them.

You can declare both ways, there is no difference actually. But, here is the thing: If you need your class to have some ivar with a special behavior or it has to be accessed from outside, etc, and it has to be a property, then I will say you have 2 options to choose from (attached to the property and inside the class interface). If that is not your case, don't create a property, is not necessary, just do it inside your class interface.

Hope it helps ;).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions