Build Your First Cocoa App
Pages: 1, 2, 3, 4
While we're here, let's modify a few things about the way our text view will act. Select the Text view and open the Inspector by choosing View Info in the Tools menu. The Info window should be labeled "NSTextView info"; if not, click on your text view again. An attributes info view should be displayed in the Inspector. If this is not shown, select it from the pull-down menu at the top of the window.
|
Now we want to enable the options for importing graphics and implementing undo services. The options are labeled "Graphics allowed" and "Undo allowed"; click the checkboxes so they are enabled.
Finally, select Size from the pop-up menu at the top of the Info window. This will allow us to set how the document window and text view will resize relative to each other when the entire window is resized. Click on the horizontal and vertical lines in the box within the window so that they look like springs. What this does is tell our application that when we resize the document window, the distance between the window border and the text view edges must always remain constant; the springs indicate that the size of the text view is free to resize so that this restriction can be accommodated.
|
The other option would be to change the lines connecting the text view edge to the window edge to springs (with the lines interior to the text view solid), which would cause the text view size to remain fixed when the window is resized, and the space between the two to freely change.
OK, so save the NIB file, and return to Project Builder where we will build and run our application, and with no coding, see all the free features provided by Cocoa.
Building and running the application
Building and running an application are achieved by clicking on the hammer and monitor icons in the toolbar (you must wait for the application to finish compiling before you run it). Alternatively you can press command-R to accomplish both of these things.
When your application starts running, you will see a blank document window appear with the name of your document in the title, "untitled". Play around with the menus and take note of what commands are available. Spellcheck. Kerning. Ligature. Undo. And there are others....
You're probably a bit underwhelmed right now because you think that those menu items can't possibly do anything useful since you didn't write any code; they're just placeholders so you'll remember to implement them later. Think again. Type some text in the window and try them out, see what happens. Have some fun with it. Play with them all!
That's right, they all work! You get text and font styling, and even spellchecking and undo all for nothing! Remember when we enabled the "Graphics allowed" option in Interface Builder? Try dragging an image file -- any image file -- onto your window. It gets pasted in there! Are you impressed yet? This stuff is amazing!
This is the power of the Application Kit and Cocoa; you get all of this just because you had the good sense to own a Mac and try out Cocoa. And we're just scratching the surface of what's possible; this won't be the last time to see something like this. Congratulations! Welcome to the world of Cocoa!
What just happened?
So how is it that we get all this stuff for free? Let me explain. When we dragged that text window onto the main window of our application, we were creating an instance of the NSTextView class in out application. Once we did that, we incorporated a wealth of code and functionality contained in NSTextView and its parent class, NSText. That is where the magic happens.
NSText is a great example of what I talked about in the first column -- Apple has put a lot of work into providing you with a rich set of tools to start out with so you don't have to reinvent the wheel. What NSText provides is code that implements all the cool things we saw: kerning, font styling, spell checking, alignment, and rulers. All of this is programmed into the NSText class, and we interact with it through instances of its subclass, NSTextView. I encourage you to take a look through the class documentation for NSText and NSTextView to gain a more detailed understanding of what's going on.
What we don't get for free
Unfortunately we can't get everything for free, but we're not doing so bad this far. You don't get to save your document yet, and open saved documents. However, it's not hard at all to add this, and the next column will focus on this.
Next time, we will take this application and add some code to it giving it some more functionality such as saving and opening files. We'll also learn more about document-based applications -- another wonderful freebie in Cocoa-as well as how to work with data.
See you in two weeks!
Michael Beam is a software engineer in the energy industry specializing in seismic application development on Linux with C++ and Qt. He lives in Houston, Texas with his wife and son.
Read more Programming With Cocoa columns.
Return to the Mac DevCenter.
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 30 of 30.
-
time to update
2005-12-20 23:24:37 RajSingh [Reply | View]
Nice gentle intro. Worth updating to XCode for the small changes in the new IDE.
Thanks.
-
Couple of minor things...
2005-06-30 22:28:45 hEADcRASH [Reply | View]
1) "you" should be "your" in "part of you interface" on page 2.
2) Please let us know what items you've added to the menubar on page 3.
3) "out" should be "our" in "in out application" on page 4.
Minor nitpicks in a great set of articles. ;-)
-
Help with the build process.
2005-03-12 19:55:20 thenewguy [Reply | View]
Everytime I try to build my project it says:
Missing file or directory: Game
Missing file or directory: 1_Prefix.h
Help!
-
hi
2003-02-05 08:33:22 anonymous2 [Reply | View]
on page 3, last paragraph, you were talking about dragging the NSTextView. i got lost there for a little while because the 'cocoa-data' name wasn't yet familiar to me.
a snapshot of the Cocoa-Data window with the text box dragged would be a nice addition to the paragraph.
very good document .. excellent. i read many and yours is the one that glue together all what i read, and got me started quickly, thanks.
-
#define error with Aug 2002 Dev Tools
2002-08-30 03:19:06 shon [Reply | View]
I'm getting this error when trying to build under the August 2002 Dev Tools (Just downloaded it tonight):
Compiling Mike Beam/SimpleTextEditor/build/SimpleTextEditor.build/SimpleTextEditor.build/Objects-normal/ppc/MyDocument.o (1 error)
<built-in>:1:1: no macro name given in #define directive
I also got this error with the Dev Tools that came with Jaguar. I did a Clean All Targets and even rebuilt the project fresh using the Jaguar Dev Tools.
What is the problem? I want to learn Cocoa but it doesn't help when a basic project can't be compiled. -
Fixed with Dec 2002 Dev Tools
2003-01-09 18:17:29 shon [Reply | View]
It appears that no one is even looking at this discussion any more as no one bothered to post a "me, too" or a workaround.
I just downloaded the Dec 2002 Dev Tools and gave it a try. This time the compile succeeds. It's surprising that Apple allowed this problem to persist despite having posted a bug-fix Dev Tools between then and now.
-
NEWBIE ALERT! Compile error on tutorial!
2002-07-22 19:39:00 eskilling [Reply | View]
Okay, I'm really new here. No previous programming experience. I'm following the instructions in the article but I get this when I try to compile & debug:
MyDocument.h:10: header file 'Cocoa/Cocoa.h' not found
MyDocument.m:25: undefined type, found `NSString'
MyDocument.m:32: undefined type, found `NSWindowController'
MyDocument.m:38: undefined type, found `NSData'
MyDocument.m:38: undefined type, found `NSString'
MyDocument.m:44: undefined type, found `BOOL'
MyDocument.m:44: undefined type, found `NSData'
MyDocument.m:44: undefined type, found `NSString'
cpp-precomp: warning: errors during smart preprocessing, retrying in basic mode
I believe I followed the instructions to the letter. I'm using Project Builder 2.0 (April Dev. Tools Beta) and rockin' 10.1.5
Regards,
Eric Skilling
-
Fix for project builder crash
2002-04-02 10:37:14 z4ph0d [Reply | View]
Use pacifist (www.versiontracker.com) to extract the missing file. Authenticate, navigate to the file, select "extract files" and your back in action!
-
Project builder won't start
2002-02-08 12:41:16 wcostain [Reply | View]
I am having troubles getting project builder to start. When I try to open it I get a zoom rect and then nothing. Looking at the console tells me the following:
dyld: /Developer/Applications/Project Builder.app/Contents/MacOS/Project Builder can't open library: /System/Library/PrivateFrameworks/JavaKit.framework/Versions/A/JavaKit (No such file or directory, errno = 2)
I looked in the directory stated for the file JavaKit, and no it is not there. I have reinstalled the developer tools to no avail. Can you suggest anything?
Will -
Project builder won't start
2002-05-14 09:06:51 mbreum [Reply | View]
Thank form me also, had the same problem..
It's very wierd: I tried to Reinstall Both DEvTools and the ENTIRE OS X with no luck... However installing onto another HD in my computer did the job; here the JavaKit framework gets installed allrigt, and Projectbuilder will run. However I wanted it to run off of the original HD so I did a 3th reinstall.. still the same.. those files missing and Projectbuilder would not launch!
Now I try this solution using pacifist!
Pass this info on to Apple.
-
not compiling! help
2001-08-05 18:42:27 videoartguy [Reply | View]
i've done everything correct.
but i'm getting an error when i try to build.
posted 08-05-2001 02:22 PM
------------------------------------------------------------------------
Build failed, check log for details.
...failed StandaloneExecutable.LinkUsingFileList /Users/videoartguy/SimpleTextEditor/build/SimpleTextEditor.app/Contents/MacOS/SimpleTextEditor ..
someone help, please!
-
A bit more digging into this app
2001-06-11 02:36:07 zeus [Reply | View]
Hy,
I'm very happy with this small text editor, but I would like to know how it is possible to change what is displayed by the about SimpleTextEditor and other menu that need to be customised.
Thanks for the help.
@+ Jerome
-
Doesn't quite work
2001-05-28 19:07:35 davehapa [Reply | View]
Very exciting stuff. I had a couple of problems with it. First double clicking the .nib file in Project Builder opened a directory rather than opening the file in Interface Builder. I opened the file in Interface Builder directly and that problem went away.
Second the application compiled and started but I could not enter any text in the window. Not sure about this one. Any hints would be appreciated.
Dave
-
AutoCompleter also works!
2001-05-26 19:23:49 plloyd [Reply | View]
Not only font utilities, spellchecking, etc, but even AutoCompleter works. I'm thrilled. This is Andrew Stone's "Build a 13 line graphical word processor" come to life on my own machine (http://www.stone.com/dev/DevCorner.html).
-
which version of ProjectBuilder and InterfaceBuilder?
2001-05-25 14:16:13 zaster [Reply | View]
I have
Projectbuilder 3.0 (v67.1)
and
InterfaceBuilder 2.0 (v187)
which i bought with 'X' ~ April 24th. Or got with system update 10.0.3? - I think?
Projectbuilder does not have the same interface, menus or buttons as in your tutorial, (the buttons look like in 'X beta") and there is no "groups & files" title for the pane.
The created .nib files don't have the same names and can't be opened from InterfaceBuilder.
At http://www.apple.com/developer/ is no other version of
Projectbuilder or InterfaceBuilder for me to download.
help? -
which version of ProjectBuilder and InterfaceBuilder?
2001-06-03 05:44:45 davehapa [Reply | View]
I assume I'm using these versions as well, since they came from the same source. I also had the problem of the .nib files not opening from Project Builder. After I opened one directly from Interface Builder the other one started behaving as expected. It seemed that X just needed a little file extension guidance.
Unfortunately, as noted in my message below, it was not enough to make a successful project.
dh -
which version of ProjectBuilder and InterfaceBuilder?
2001-05-30 10:15:13 Michael Beam |
[Reply | View]
Sorry for the dealy in a reply. I'm in Europe right now and will be until the end of June. I can't check my version of project builder on my computer, but i just noticed in the first screenshot of this article are the PB release notes, which indicate i was using PB 1.0. Unforunately i can't investigate this much farther. If anyone else could help us out of this little snag i would greatly appreciate it. Thanks!
Mike
-
PB crashes b4 new proj window
2001-05-23 19:06:24 gtwood [Reply | View]
After I enter 'SimpleTextEditor' and click Finish, Project Builder crashes. Any ideas? I'm on X 10.0.3 on a B&W G3/400 w/ 512 MB ram.
-
Keep it up
2001-05-21 07:49:01 bradrice [Reply | View]
This was a great column to get our feet wet. Keep it up. I look forward to this article. Apple really did provide us with some nice developer tools. I hope you will eventually cover the other tools as well. Or perhaps another column topic could cover them. Is the plist editor essentially an XML editor?
Brad Rice -
Keep it up
2001-05-21 09:21:55 Michael Beam |
[Reply | View]
I'm glad you're liking the columns! I'll see what i can come up with as far as the other dev apps go. As far as the plist editor goes, i don't think it handles all things XML, just property lists. But then, property lists are used quite extensively in Cocoa.
-
Excellent !
2001-05-20 10:47:16 zenarcade [Reply | View]
I am really looking forward to the next section. I really hope that the Cocoa column can stay on the net for a logn long time
tom -
Excellent !
2001-05-20 23:14:14 Derrick Story |
[Reply | View]
I can tell you with a certain amount of confidence that the Cocoa column will be around for some time :) -- Derrick
-
So what's happening next time?
2001-05-17 22:29:52 Derrick Story |
[Reply | View]
When I first read this column, I was really pleased to see that we're working on a real project now. So are we going to finish this application in the next column? What's in store for us? Will it be a real working Text Editor for Mac OS X? Could you give us a preview? -
So what's happening next time?
2001-05-17 22:59:25 Michael Beam |
[Reply | View]
Yeah, i'm glad we got to a real app too! I like this column and the next because of the way they fit together and work with each other. In this column we simply built the interface to the text editor, but because of the functionality of Cocoa, this interface is more than an interface. Of course, saying that this first app is a fully functional OS X app is a tad of a stretch (take it how you like it :); we can't even save or open files yet! In the next column this is exactly what we will enable our applicaiton to do. As an aside, i think this is a pretty cool thing about Cocoa, that we can start out learning how to save and open files and work with data, rather than the standard "Hello, world!" thing. I hope everyone is enjoying it all! Thanks for reading! -
So what's happening next time?
2001-05-17 23:21:25 Derrick Story |
[Reply | View]
Good point about the "fully functional" blurb. I guess I should have said, "semi-functional." Better yet, maybe I'll adjust the blurb now and leave out the sizzle ;-)








Thanks in advance,
Remco Poelstra