Connection IBOutlets, variable, property, or both?

When using IB in conjunction with an additional view, you control the dragging and dropping of the element in IB into the .h file and create an output. You can drag it to one of two places, either inside the variable declaration block or outside the block.

If you drag it inside a variable block, you will 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 thought about how different they are, and I'm a little confused. Well, I understand that the synthesized properties have some magic and create instance variables at run time (only at 64 bit / ARM). Therefore, I believe that I understand how 2 options work.

Which is the best option? The first option generates less code and seems simpler.

The second version offers publicly available accessors / mutators, but I rarely get access to exits from outside my class (and, if so, almost always with encapsulation). Since I started working with iOS, I have used this option exclusively.

Am I missing something or should I make the switch to variable base outputs in most cases?

+4
source share
3 answers

Short answer: It does not really matter in any case.

Long answer: If you need set / mutator methods, drag outside the block. If you don't care about the methods and just want to access the variables directly, then placing them as direct variables inside the block is probably the way to go.

Public Visibility : If you simply specify an IBOutlet as a variable, you can use @protected or @protected to prevent external access. If you really want @property for some reason you can still control public visibility by moving the property from .h and to the class extension in the .m file.

Conclusion:. I stick to declaring a direct variable and keep other parameters when I need something extra.

+1
source

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

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

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

 @interface MYCLASS() @end 

and declare your properties there. This is my preferred way of doing this if I don't need open access to this property (which should be the exception, not the norm, if you obey the MVC conventions).

+1
source

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

Remember, you can have both. The inside of a variable block is essentially only used when you use it in IBActions.

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

Again, you can use both options, it depends on how much you use it.

0
source

Source: https://habr.com/ru/post/1400614/


All Articles