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?
Short answer: It doesn’t make a much of a difference either way.
Long answer: 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
IBOutletas a variable then you can use@privateor@protectedto prevent outside access. If you really want a@propertyfor 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.Conclusion: Myself, I’m sticking with the straight variable declaration and save the other options for when I need something extra.