Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MadDoc

macrumors 6502
Original poster
Apr 25, 2005
329
5
UK
I'm learning OnjC and Cocoa. My C is not my strongest point. I am trying to build a simple single window app to teach me about using delegates.

All the app needs to do is keep the window twice as tall as it is wide when it is resized. I have one class (AppController) that has been defined and set as the delegate for the NSWindow.

This is the windowWillResize method

Code:
#import "AppController.h"

@implementation AppController

// Delegate methods
-(NSSize) windowWillResize: (NSWindow *) sender toSize: (NSSize) frameSize
{
	// Make sure that the window remains twice as tall as it is wide
	 NSSize newSize;
	
	newSize.width = frameSize.Width;
	newSize.height = frameSize.width * 2;
	
	return newSize;
}

@end

This doesn't compile however as I get this error:'NSSize' has no member named 'Width'.

I don't fully understand what I've done wrong - I thought NSSize was a C struct with 2 variables (height and width).

What do I need to change?

MadDoc
 

MadDoc

macrumors 6502
Original poster
Apr 25, 2005
329
5
UK
Ooops.

Thanks!

BTW, will my approach to the problem work with this method (can't check 'til I finish work in 10 hours!).

MadDoc,
 

MadDoc

macrumors 6502
Original poster
Apr 25, 2005
329
5
UK
Is setAspectRatio a delegate method too? If not, I am just trying to learn about delegates (it's a new concept to me). Would my method (would the corrected 'W'idth) work?

Thanks for the tip.

MadDoc,
 

Krevnik

macrumors 601
Sep 8, 2003
4,101
1,312
Just use the built-in NSWindow method setAspectRatio:

I wouldn't use it, it doesn't behave as one expects. What the OP is trying to do is the best way to keep it smooth and not run into issues with setAspectRatio: causing odd resizing behavior.

EDIT: One thing I /would/ change is instead of calculating the height from the width, calculate the width from the height. Introduce as little deviation with where the mouse pointer is (compared to the resize control's location) as possible.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.