12

sorry If am I being too picky on this one but I am learning iOS programming now and I've seem some people who declare the IBOutlet like this:

IBOutlet attached to property

#import <UIKit/UIKit.h>
#import "CustomCell.h"

@interface CustomTableViewController : UITableViewController {
  CustomCell *customCell;
}
@property (nonatomic, retain) IBOutlet CustomCell *customCell;
@end

And some declaring like this:

IBOutlet attached to the declaration inside the interface

#import <UIKit/UIKit.h>
#import "CustomCell.h"

@interface CustomTableViewController : UITableViewController {
  IBOutlet CustomCell *customCell;
}
@property (nonatomic, retain) CustomCell *customCell;
@end

which one is the proper way to declare it? Are any differences between them? If someone know to explain why do they put it on different places it would be awesome to learn.

Thanks a lot :)

Community
  • 1
  • 1
zanona
  • 12,345
  • 25
  • 86
  • 141
  • possible duplicate of [Are @property's necessary for Interface Builder?](http://stackoverflow.com/questions/4579699/are-propertys-necessary-for-interface-builder) – Dave DeLong Jan 08 '11 at 19:13

3 Answers3

12

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.

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
Jonah
  • 17,918
  • 1
  • 43
  • 70
1

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.

Steven Kramer
  • 8,473
  • 2
  • 37
  • 43
0

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 ;)

nacho4d
  • 43,720
  • 45
  • 157
  • 240