Chp 5 Challenge Solution

Thursday, October 06, 2005

In continuation of the sharing of my solutions for challenges shown in Cocoa Programming for Mac OS X, here's chapter 5. The challenge is to create an application to demonstrate the use of a delegate for a NSWindow that keeps the height at twice the desired width.

Start out by creating a new Cocoa Application project.

Once ready open the MainMenu.nib file from the project in Interface Builder. In the classes tab of the MainMenu.nib palette, create a subclass of an NSObject and name it "WindowResizer". Now create an instance of the WindowResizer class. Once that's done, switch to the Instances tab, and control drag from the Window to the WindowResizer instance, and connect the WindowResizer object as the delegate of the window.

Now we need to create the files for the WindowResizer class. To do so, go back to the classes tab and select the WindowResizer class we've added. Now create the .m and .h files adding them to your project. (Classes menu option)

Save and close the MainMenu.nib file.

Now we need to go to XCode and edit the WindowResizer.m file. Add the following method to the implementation block.

- (NSSize)windowWillResize:(NSWindow*)sender
toSize:(NSSize)frameSize {
return NSMakeSize(frameSize.width, frameSize.width * 2);

Compile and run the application and you should see the window resize with the contraint we added. One thing you'll probably notice is that the first time we resize the window shifts abruptly, because the starting dimensions don't jive with the constraint so the first time the delegate message is sent, the window will shift quite a bit. To lessen the shock you could go back to the nib file and have the window start with a width of 250 and a height of 500, or some other dimension that match the constraint.

Happy Cocoa Travels!

Have you noticed any quirks or differences between programming XCode on 1.5 and 2.1? I know I'm getting ahead of myself, but I'm having a few difficulties with the sample program in Chapter 6.
Outside of expected functional differences, the only bad thing I've noticed is that occasionally when I'm using the All-In-One mode for XCode, I get the applicaiton into a state that won't allow me to edit my code properly. This used to happen quite often, but lately it seems to have subsided with the latest updates. I say subsided because it still happens, but not nearly as much.

Are you running into anything particular?
One of the problems that I ran into, is setting the Window delegate. I tried control dragging from the WindowResizer object I created and the Window in IB. Problem is, there were no connections that I could make to the NSWindow. Any suggestions?
Hi Richie,

Actually, it sounds like you got things flipped. You want to make the resizer the delegate of the window. So you should control drag FROM the NSWindow instance TO the WindowResizer instance and make the resizer instance the delegate outlet of the window.

That should do it.
This comment has been removed by a blog administrator.
You were right. I was control dragging in the wrong spot. Instead of in the instances window, I was dragging directly from the NSWindow itself to the WindowResizer.

I guess that's why they call it learning. Thanks for the help.
i just found these posts and i want to thank you for them. i've been through a few chapters of aaron's book and i just got approval to go to the cocoa class at bnr. these will come in handy.
Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?