¿Conectando IBOutlets, variable, propiedad o ambos?

When using IB in combination with assistant view you control-drag an element in the IB to the .h file and create an outlet. You can drag it to one of 2 place, either inside the variable declaration block or outside the block.

If you drag it inside the variable block you get something like this:

@interface MyViewController : UIViewController {
    IBOutlet UIButton *foo;

dragging it outside the block gives you something like....

@interface ViewController : UIViewController {
@property (retain, nonatomic) IBOutlet UIButton *foo;

I've thought about how they are different and I'm a little confused. Ok, I understand synthesized properties do some magic and create instance variables at runtime (only on 64bit/ARM). So I believe I understand how the 2 options work.

What's the best option though? First option generates less code and seems simpler.

Second version offers public accessors/mutators, but I rarely access outlets from outside my class (and if I do, it's almost always with encapsulation). From the time I've started iOS work I've exclusively used this option.

Am I missing anything or should I make the switch to variable based outlets in most cases?

preguntado el 09 de marzo de 12 a las 16:03

3 Respuestas

The ability to declare IBOutlet in the property declaration is relatively new @property (retain, nonatomic) IBOutlet UIButton *foo;

Previously, you had to declare IBOutlet UIButton *foo inside the curly braces and then synthesize the property. Now, declaring the IBOutlet in the curly braces is redundant.

You have two options to declaring the properties now. Option 1 is to declare it in your .h file, which will make it public. Alternatively, you can create a private interface in your .m file using:

@interface MYCLASS()

and declare your properties there. This is my preferred way of doing it unless I need public access to that property (which should be the exception, not the norm if you are obeying MVC conventions).

Respondido 10 Abr '12, 16:04

I use class extensions heavily, but I never thought about using them for IBOutlets. I just did some testing on this and IBAction work in class extensions (which is even better), but the Xcode support is pretty bad. It insists on stuffing the method declaration in the .h and you have to manually move it to the .m file. +1 for the ideas. - DBD

Yes, I suppose I should clarify. If you want to drag using the classic Xcode way, you have to put them in the .h file. However, there is another way which is even easier, in my opinion. For IBActions, you don't even need to declare them in an interface section. Just implement your IBAction in your .m file like - (IBAction) buttonPressed: (id) sender {}; then use the assistant editor and you can ctrl-drag from your storyboard button to the IBAction implementation to link. For private IBOutlets you can ctrl-drag from the storyboard to your private interface @interface MYCLASS() @end. - Mike Z

Respuesta corta: It doesn't make a much of a difference either way.

Respuesta larga: If you want set/mutator methods, then drag outside of the block. If you don't care about methods and are just going to access the variables directly then putting them in as straight variables inside the block is probably the way to go.

Public visibility: If you just specify the IBOutlet as a variable then you can use @private or @protected to prevent outside access. If you really want a @property for some reason you can still control public visibility by moving the property out of the .h and into a class extension in the .m file.

Conclusión: Myself, I'm sticking with the straight variable declaration and save the other options for when I need something extra.

Respondido 16 Abr '12, 17:04

IBOutlets are best inside of the block, unless you really plan on working with it in the .m file.

Remember, you can have both. The one inside of the variable block is essentially, in all basics, just for when you use it in IBActions.

The property can be used in the .m file for further customization.

Again, you can use both, it just depends on the extent you're using it.

respondido 09 mar '12, 16:03

what IBOutlets would you make you don't use in the .m? They aren't really needed for IBActions since the action pass in a reference. Why would I want both at this point? If compiler will add the instance variable for me at runtime and manage it for me when I use a property, is there a use to adding it myself manually? - DBD

You would only want both if you have something, such as a webview, that requires further implementation in the .m. You'd need the variable outlet to connect it for actions, and the property to add functions such as a home page, loading requests, etc - JTApps

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.