An Introduction to RubyCocoa, Part 1
Pages: 1, 2, 3
Creating the Controller Class
Just like a normal Objective-C controller class in a Cocoa application, our Ruby Controller class must inherit from the NSObject class. So, our first step is to subclass the NSObject class. Find the NSObject class in the "Classes" tab of the "MainMenu.nib" window.
Select this class in the list and press the "Return" button. This should create a new subclass of the NSObject class called MyObject. Highlight the new NSObject child class, if it is not already highlighted, and change its name from MyObject to Controller.
Once we've created and renamed our new Controller class, we need to add the necessary actions and outlets to it. Make sure the Controller class is highlighted and open the Info window (you can do so by selecting the "Show Info" item under the "Tools" menu, or by using a "Shift-Command-I" key combination) and select "Attributes" from the drop-down list.
First we'll add the outlets. So, click on the "Outlets" tab and add five new outlets to the list: 1) fileTableView, 2) archiveFile, 3) fileType, 4) fileTypeView, and 5) mainWindow.
After adding all of our outlets to our Controller class, we need to add the actions that it will be tasked with performing. To do this, we click on the Actions tab and add the following actions to the list: 1) addFile, 2) removeFile, 3) createArchive, 4) extractArchive, and 5) browseForArchive.
We now have our Controller class fleshed out enough to let us start making all of our connections between the nib file we've created and our Ruby application. Before we proceed, however, we must create an instance of our Controller class. We do this by selecting our Controller class from the list under the "Classes" tab in our "MainMenu.nib" window. Then, you can either control-click on it and select "Instantiate Controller" or you can select it through the "Classes" menu in the Interface Builder menu bar.
Once we have an instance of our Controller class, we can begin making all of our necessary connections. In the process we'll also find out what each of the outlets and actions do in our application. We'll begin with the outlets we created and then move on to the actions afterward.
The first three outlets are used to update and access the information the user shares with us through the interface. The latter two are needed only for using the NSOpenPanel and NSSavePanel classes. You'll need to connect each of the outlets to their respective interface elements. This is done by control-clicking on the newly created instance of our Controller class and dragging a line from it to the GUI element it represents.
The fileTableView outlet should be connected to the NSTableView object under the "Create" tab that displays the files we have selected for archiving. The archiveFile outlet should point to the NSTextField (text box, not label) under the "Extract" tab that will hold the name of the archive file we have chosen for extraction. The fileType needs to be connected to the NSPopUpButton that we placed in our custom view in order to allow us to access the file type chosen by the user when in the process of creating a new archive file. The fileTypeView outlet is a pointer to our custom view we created for the NSSavePanel, and finally, our mainWindow outlet is a pointer to the main window of our application.
You now have half of your Controller class' connections created. Next, we'll need to attach each of its actions to an element of the interface. Let's take a look at what each is supposed to do, and to which item each will be attached.
Connecting actions to the GUI elements that trigger them is done
slightly differently than connecting outlets to their GUI elements.
Rather than control-clicking the Controller object and
dragging the line to the GUI element, we are going to go backwards, and
control-click the GUI element that triggers the action and drag a line
to the Controller object. Let's start with the actions
for the '+' and '-' buttons.
The addFile action calls up an instance of the
NSOpenPanel and allows the user to select one or more
files that they want to include in their archive. The
removeFile action simply deletes the currently selected
file from the table. We need to connect both of these actions to the
'+' and '-' buttons underneath the NSTableView object in
our "Create" tab.
The createArchive and extractArchive actions
are responsible for creating new archive files and extracting the
contents of an already existing archive file to a chosen directory. The
first action displays an NSSavePanel allowing the user to
select a location for—and assign a name to—the archive file
being created.
We should create a connection between it and the "Create Archive"
button on the "Create" tab using the same method that we did for our
add and remove file actions. The extractArchive action
executes the tar program to extract the selected archive
file to the directory chosen by the user through an instance of the
NSOpenPanel class. The "Extract" button should be
connected to the extractArchive method in the same way as
the "Create Archive" button.
Finally, the browseForArchive action allows the user to
select an archive file for extraction using an NSOpenPanel
object. It is called whenever a user clicks on the "Browse" button
under the "Extract" tab, and therefore, needs to be connected to that
button following the same routine as described earlier.
That takes care of about everything that we can do using Interface
Builder. The rest of our work will need to be done in Xcode. Normally,
at this point we would select our Controller class and
have Interface Builder create files with skeleton code for us to fill
out in Xcode. However, it will only do this for Objective-C and Java,
not Ruby. So, we'll have to do a little bit of work by hand here. So
make sure you've saved your nib file before closing Interface Builder
and Xcode.
The next installment will deal with creating the skeleton code that Interface Builder usually creates for us and finishing our project by adding in the rest of the Ruby code needed to give us a functioning application.
Until then, I hope this tutorial keeps you busy!
Christopher Roach recently graduated with a master's in computer science and currently works in Florida as a software engineer at a government communications corporation.
Return to MacDevCenter.com.
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 11 of 11.
-
yep, there it is:
2007-06-08 06:00:54 bj0ern [Reply | View]
http://www.oreillynet.com/pub/a/mac/2004/10/12/cocoa.html
-
RubyCocoa Binary for Panther (OS X v10.3)
2004-11-16 06:44:19 Christopher Roach |
[Reply | View]
Hi all,
I just wanted to let anyone know who may still be having trouble installing RubyCocoa on their Mac's running OS X Panther edition that I found a link to download a binary version. You can download the disk image at the following URL.
http://homepage.mac.com/kimuraw/archive/RubyCocoa-0.4.1-panther.dmg
I hope this binary installer will help any of you out there still having trouble.
Cheers
-
Browse TextField
2004-10-16 12:21:12 lucbelanger1 [Reply | View]
The TextField beside the Browse button needs to be aNSTextFieldand not anNSTextViewas mentionned. The NSTextView class does not respond to setStringValue.
NSTextView#setStringValue: - methodSignature is nil. (OSX::OCMessageSendException)
-
Browse TextField
2004-10-16 16:52:56 Christopher Roach |
[Reply | View]
You're absolutely correct. That is completely and totally a typo on my part. The good news is that the provided Xcode project is using an NSTextField and not a NSTextView. So, I got the code right, but I made a typo in my article.
Nevertheless, I do thank you for pointing that out to me and I have just sent off an email to the editors at O'Reilly requesting a change in the text of the article if possible.
Thanks for the help, its very much appreciated.
-
Now the config step is failing <sigh />
2004-10-07 21:03:15 gvaughn [Reply | View]
ruby install.rb config is failing with this message:
create /Users/gvaughn/development/rubycocoa-panther/framework/src/objc/osx_intern.h ...
cd src/objc/cocoa; ruby gen_cocoa_wrapper.rb
sh: line 1: cd: src/objc/cocoa: No such file or directory
ruby: No such file or directory -- gen_cocoa_wrapper.rb (LoadError)
config failed
hook /Users/gvaughn/development/rubycocoa-panther/framework/post-config.rb failed:
'system cd src/objc/cocoa; ruby gen_cocoa_wrapper.rb' failed
-
Now the config step is failing <sigh />
2004-10-07 22:12:32 Christopher Roach |
[Reply | View]
Ok, the error your getting is when you tell the Ruby interpreter to run a file that does not exist on your computer. I'll bet if you cd into src/objc/cocoa from your rubycocoa-panther directory (that was created durring the CVS session) and do an "ls" you'll find that the file gen_cocoa_wrapper.rb is nowhere to be found. More than likely it was not created during the CVS steps.
So, I would remove the entire rubycocoa-panther directory and run through all of CVS steps again and try to do the ruby install after that. If memory serves, I actually had to get the files from CVS twice the first time I installed RubyCocoa on my system because the first try didn't download all of the files I needed.
So, give that a try and if you're still having problems with it, send me an email (just click on my name and it should bring up OSX Mail) and I will tar up the rubycocoa-panther directory I have and send it to you and we can take it from there.
Don't give up me just yet, we'll get everything working on your system before you know it.
Good luck, and let me know what happens.
-
cvs problem
2004-10-06 20:53:30 gvaughn [Reply | View]
That second cvs command is causing me problems. I get an error message:
cvs server: cannot find module 'rubycocoa-panther' - ignored
cvs [checkout aborted]: cannot expand modules
any ideas? -
cvs problem
2004-10-07 20:56:47 gvaughn [Reply | View]
My apologies. I had originally manually typed the line removing the backslash. I re-read it carefully several times, but must have missed something. After doing a pure copy and paste, it's checking out the files. -
cvs problem
2004-10-06 22:03:39 Christopher Roach |
[Reply | View]
Ok, I just remembered a problem I had with this command the first time I tried it. The backslash at the end of the first line is for line continuation. So, if you type the command in by hand, leave out the backslash. Otherwise, just copying the entire command (backslash and all) and dropping it into Terminal should work.
Hope that works. -
cvs problem
2004-10-06 21:57:05 Christopher Roach |
[Reply | View]
Hmm, that's interesting. I just tried it again to make sure that the CVS commands haven't changed since I installed my copy and everything seemed to work fine. Make sure that you left the password blank in the login step (step 1) and then highlight the second command and drag and drop it into your terminal window to make sure everything is copied exactly the same. If you drag and drop all of the commands over to the terminal it should work. (BTW, holding down the command button while clicking on the highlighted text in the webpage in the background will allow you to drag it over to the terminal without losing focus on the terminal window.)
Let me know if that doesn't work and we'll try something else.





