Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Detailed Clarification on CVs .m, and .h | Custom Class .m, .h, and their relationship with IBOutlets, IBActions, Logic

Hello:

I need clarification of .m and .h files. I remain completely confuser about their purpose. I understand that you can create your methods and properties in .h files, and then place that logic in the .m files.

When creating custom classes, I am unsure of the proper protocol for stating methods, IBOutlets, IBActions, properties, and all of that in the proper location.

I am also confused about the view controller. Is there often only one of these?

What if my project has multiple views -- do I need a a custom ViewController for each view?

It would be wonderful to see your perspective and understanding on this topic.

3 Answers

1.Properties and methods are defined in the .h file below the @interface line?

  1. Logic is implemented in the .m files. Does Xcode generate a template/code block for methods and properties that are defined in the .h in the .m -- or do I need to manually code the names and perform the logic?

  2. Private methods and properties should be defined in the .m file for that scope, instead of the .h.

  3. IBOutlets are defined in the .m file, and we also implement the logic in the .m file below the @implementation line?

  4. IBActions are defined somewhere in the .m file/below the @implementation file?

  5. Each view has a ViewController, or should for proper management. Example: Splash (splashViewController), login (loginViewController), home (homeViewController), etc.

  6. Can IBActions also call other IBActions for more complex interactions?

Stone Preston
Stone Preston
42,016 Points
  1. The only time Xcode automatically generates code more methods defined in the .h is when you add an IBAction from your storyboard. it will add the declaration in your .h, and then it will add the name and empty {} in your .m

  2. yes, but private methods and such are a bit of strange thing in objective C. they arent really "private", but thats a bit more than you probably need to know at this point. Honestly I hardly every use class extensions. most of my properties and methods are almost always declared in the .h

  3. IBOutlets are declared in the .h. you can reference them in your code in the .m

  4. IBActions are implemented in the implementation below the @implementation, when you connect a button from your storyboard to your class as an action, it will automatically get added to your implementation with the name and empty { }

  5. yes generally each view has its own view controller, however there are cases where that is not always true.

  6. not sure about that, I have never tried. its probably possible. generally you want an IBAction to be the thing that happens whenever you press a button, tap a control etc. If you need it to do a couple things you would usually put that inside a separate method that is called from inside the IBAction.

Stone Preston
Stone Preston
42,016 Points

.h files are used for public property and method declarations, whereas .m files are used for the implementation of those. Properties and methods provided in the .h file are public and can be seen by other objects. You can add a "private" (private is a bit of misnomer in objective c but I wont get into that) class extension to the .m file to add "private" properties and methods for stuff you dont want anything other than your class to know about.

a class extension looks like this:

//in a .m file
@interface SomeViewController ()

//this property is private
@property (nonatomic) NSArray *someArray

@end

@implementation SomeViewController

//implement everything here

@end

if the properties and methods only need to be known by the class you are declaring them in, make them private by declaring them in an interface within the .m, if something else needs to know about them, make them public and declare them in the interface in the .h

most apps have multiple view controllers. Normally you have a view controller for each view in your app. So if oyu have login screen you have a login view controller, if you have a home screen you have a home view controller etc etc

Thank you for the review.

Could you explain where I connect, define, and implement IBOutlets and IBActions, and where the logic is placed. I presume that we can define IBOUtlets/IBActions in the .h files, and implement the logic in the .m file?

Thank you for the comments. That cleared a lot!!