Best practices of IBOutlet

I would like to know which place is best for placing IBOutlets from the storyboard:

a) In the header file (.h)

b) In the extension of the class created on the .m file

thanks

Hi

+5
source share
5 answers

You should keep in mind that .h is a publicly available header.

So put IBOutlet there if they should be accessible by other classes.

However, although you can do it. I would say publishing an IBOutlet in a publicly available heading is not good practice (in terms of object orientation), as you reveal some implementation details that should only be visible to anyone.

In short, placing an IBOutlet in a .m class .m is good practice.

+11
source

Apple 's Resource Programming Guide: Nib Files :

Outputs are usually considered closed to the defining class; if there is no reason to publicly publish this property, hide the class extension property declarations.

+7
source

A class extension is the best place if you do not want to publicly publish this publication. Your .h should be neat and clean and should contain only those methods or properties that are publicly available (available to other programmers). This way you will not confuse your teammate without having unnecessary ivars and slowing down methods in the .h file

It's all about managing code and reducing confusion, otherwise there are no private methods / properties in Objective-C

Also, if you check any apple sample, they will follow the same pattern. e.g. LoadingStatus.m has code

 #import "LoadingStatus.h" @interface LoadingStatus () @property (nonatomic, strong) UIActivityIndicatorView *progress; @property (nonatomic, strong) UILabel *loadingLabel; @end 
+4
source

Assuming this is for the view controller, option b is better since you should not publish public publications for other classes that you can directly interact with. They should be considered your private knowledge. Your controller should provide a different and more suitable interface.

If this looks a little grayer, how should you approach the problem, since MVC will lead you to expose outputs to allow the controller to use them. MVVM will lead you to hide the outputs so that the view is passed to the view model object and internally adjusts the outputs.

+3
source

@interface can be displayed both in the .h (public properties) file and in the .m file (private properties). IBOutlets should be declared in the .m file.

If you are interested to read this topic .

Hooray!

+1
source

All Articles