Jump to content

Some noob Cocoa help


2 posts in this topic

Recommended Posts

I've been reading Aaron Hillegass' Cocoa Programming For Mac OS X (3rd edition), but I've ran into a small problem...At the end of chapter 6 the one of the challenges is to create basic application that has one window, and an object that is a delegate of the window. "As the user resizes the window, make sure that it is always twice as tall as it is wide"...I'm supposed to use this delegate method: - (NSSize)windowWillResize:(NSWindow *)sender toSize(NSSize)frameSize.

 

...I have no clue how to use his method...the documentation isn't very helpful in this case either (or maybe i just don't know where to look). I would really appreciate any help.

Link to comment
Share on other sites

This is pretty easy. Create a new Cocoa application. Add a new obj-c class called AppController. Open your apps xib file with IB. In IB, add a new NSObject and set it to AppController. Set the main windows delegate outlet to AppController.

Change the size of the window so its width is half its height plus 11. Save the xib.

 

Edit your AppController .m file to look like this...

#import "AppController.h"


@implementation AppController

- (NSSize)windowWillResize:(NSWindow *)sender
				toSize:(NSSize)frameSize {

NSSize newSize; // This will be the size the window is actually changed to.
newSize.width = frameSize.width; // The width stays the same...
newSize.height = (newSize.width * 2); // But the height needs to twice the width.

NSLog(@"width: %f", newSize.width); // Log the results.
NSLog(@"height: %f", newSize.height);

return newSize; // Return the new size.
}

@end

 

This code it pretty self explanatory. When ever the user tries to resize the window this method gets called. "sender" is the window that is being resized. "frameSize" is the size it would resize to if you didn't modify anything. The NSSize structure you return is the size that the window will resize to.

 

Attached to this post is an Xcode project to look at if you get stuck.

TwiceAsTallAsWide.zip

Link to comment
Share on other sites

 Share

×
×
  • Create New...