Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Build a Simple iPhone App with Objective-C Views and View Controllers IBOutlet

Why is Label dragged to .h file and is Button dragged to .m file?

I don;t know why it is different from each other!

Jose Ramos
Jose Ramos
1,908 Points

I highly recommend taking the pre-requisite to this course, Objective C Basics. It will REALLY help in understanding these videos and I think you are severely hurting yourself if you don't go through them.

.h is seen as a 'declaration' file, where .m is seen as an 'implementation' file.

When you drag the connect the button 'Show Another Fun Fact' over to Viewcontroller.m, it creates a method, showFunFact. At first, we had nothing in this method (ie, clicking the button would do nothing). We initially added NSLog, which would print a string of our choice to the console when clicked.

Now, this may not be the best explanation, but the label is dragged to .h because we are DECLARING the label here (as a property). It is of the class UILabel and is declared as a pointer (the asterisk) so we can reference it. To 'implement' this label with the button, we need to do this in the .m file: the implementation file. This is done in the showFunFact method:

self.funFactLabel.text = @"another interesting fact!";

The way I try to look at it is this:

.h files you can declare stuff, but if you want to do cool shit with these things, you have to implement them in the .m files. I see .m files as 'under the hood'.