8

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

a) In the header file (.h)

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

Thank you

Regards

Wain
  • 118,658
  • 15
  • 128
  • 151
apinho
  • 2,235
  • 3
  • 25
  • 39

5 Answers5

14

You have to keep in mind that .h is a public header.

So place your IBOutlet's there if they should be accessible by other classes.

However, even though you can do that. I would say that exposing the IBOutlet's in a public header is not a good practice (From object orientation perspective) since you are exposing some implementation details that should be only visible to whom is concerned about.

In short, placing the IBOutlet's in a class extension in the .m is a good practice.

Otávio
  • 735
  • 11
  • 23
8

From Apple's Resource Programming Guide: Nib Files:

Outlets are generally considered private to the defining class; unless there is a reason to expose the property publicly, hide the property declarations a class extension.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
4

Class extension is the best place if you don't want to expose that outlet publicly. Your .h should be neat and clean and should only contain those methods or properties which are public (available for other programmers). In this way you won't confuse your teammate by not having unnecessary ivars and methods deceleration in .h file

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

Also if you check any sample of apple they 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
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
3

Assuming that this is for a view controller, option b is better as you shouldn't be exposing the outlets publicly for other classes to directly interact with. They should be considered your private knowledge. Your controller should expose a different and more appropriate interface.

If it's a view it's a bit more grey how you should approach the problem as MVC would lead you towards exposing the outlets to allow the controller to use them. MVVM would lead you towards hiding the outlets so that the view is passed a view model object and internally configures the outlets.

Wain
  • 118,658
  • 15
  • 128
  • 151
1

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

If you are interested read this topic.

Cheers!

Community
  • 1
  • 1