I have moved my blog to Wordpress at theunixgeek.wordpress.com. I will still be checking back periodically on this one as well, though. 19 April 2009

featured

Merging Mkdir and Cd | 280 Slides Interview | I Switched to KDE 4

clickable portals

Sunday, November 11, 2007

The Cocoa Tutorial Everyone Needs

Thank you to Thomas Davie for help with this article!

I have yet elsewhere to see a Cocoa tutorial that explains how to, when you click a button, make the text in a text box be "Hello World". Here it is. As a special 100th post for The Unix Geek, I will show you how to make a simple Cocoa app that does the above mentioned.

1. Fire up Xcode. File> New Project. For our purposes, name it HelloWorld.
2. File>New File. Select Objective-C class under Cocoa. Click "Next", name the files HelloWorldController, and click "Finish".
3. You should now have HelloWorldController.h and HelloWorldController.m Go into the .h file and have this as the file contents:

#import <Cocoa.h>

@interface HelloController : NSObject {
IBOutlet NSTextField *words;
}
- (IBAction)sayHello:(id)sender;
@end


Now, you're probably thinking, what does this do? First off, make sure you have an understanding of object-oriented programming (see about 5 posts ago). Now, pay attention. The 3rd line creates an instance of the object NSTextField called "words". The 5th line defines a method, or action. If you're familiar with functional languages, you might think of it as a function, in a sense.

4. Open the MainMenu.nib file. Design your interface here. Put (on the window) a button (you can edit the text) and a text field (keep it empty). Now, look for NSObject and drag that in to the window where it says "MainMenu.nib" as the title.

5. Open the inspector (Command + Shift + I), and with NSObject selected in the second tab from the right, in the first text field, type in "HelloWorldController".

6. Holding the control key, drag a line from NSObject to the text field. Select "window". Drag a line (again holding control) from the button to NSObject. Select "sayHello:"

7. Now that all parts of your app are hooked up, let's write some code. Make sure your HelloWorldController.m file looks like this:

#import "HelloController.h"

@implementation HelloController
- (IBAction)sayHello:(id)sender{
[words setStringValue:@"Hello, World"];
[words setNeedsDisplay:YES];
}
@end


Notice the [square brackets]. These are a central part of Objective-C. If you read the post on object-oriented programming, you should know how these messages work.

8. Select "Build and Go" from the toolbar (make sure you've saved your project first).

9. Press the button and watch it work!

10. If you'd like to distribute your app, check under [wherever you saved your project]/build/Release and your app should be there.

3 comments:

Kevin said...

Hi - I have a new blog on Blogger also, and I'm trying to figure out the Digg buttons. Can you tell me how you programmed your Digg code on the side panel? I see it changes based on the page that is loaded. Can you send me the code to use on my site? Thanks...

elliottcable said...

Less than and greater than signs - use this:

#import &lt;Cocoa.h&gt;

(On a side note, to post *that*, I used this code: #import &amp;lt;Cocoa.h&amp;gt;)

Will said...

Great post, needed this as a C/C++ programmer new to Objective-C and Cocoa!