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

ffej2ffej

macrumors newbie
Original poster
Jan 27, 2009
5
0
Southern California
I am reading COCOA PROGRAMMING FOR MAC OS X by Aaron Hillegass (and learning a LOT by the way). I am attempting one of the challenges in the book, that is, making a window resize itself so it is always twice as wide as it is high. The challenge was to make a "helper" object that would take on the delegate duties for a NSWindow. I've done that and even made the object react when the window is resized. I cannot, however, seem to get the dimensions of the newly resized window in order to complete the task. I've tried a few different things, and I keep getting the same (obviously wrong) dimensions. The height is 0 and the width is some large number over a billion. The delegate method that I'm using is
-(NSSize)windowWillResize:_(NSWindow *)sender
toSize:_(NSSize)frameSize
and I've tried to access the size using sender.frame.width, frameSize.width and theWindow.frame.width. Please tell me how I can get this information accurately.
 
It's doable

Hmm... I remember doing this successfully when I stepped through parts of Hillegass' book. So it is possible. Since the numbers you're seeing are weird, perhaps the variables are not integers? Or maybe you're writing to NSLog and using the wrong format character?

If I get time (hello college football season) I may be able to dig through my saved code from the Hillegass' book. Sorry I'm not more help today.
 
here is the code

Post your code.

Here is the method in question. The lines commented out are things I tried that didn't work.

-(NSSize)windowWillResize:_(NSWindow *)sender//I know the underscore
toSize:_(NSSize)frameSize//doesn't belong there
{

NSRect frame = [sender frame];
NSSize mySize;
mySize.width = frameSize.width;
mySize.height = frameSize.height;
// mySize.width = theWindow.frame.size.width;
// mySize.height = theWindow.frame.size.height;
// mySize.width = sender.size.width;
// mySize.height = mySize.width / 2;
NSLog(@"The width is %d and the height is %d",mySize.width,mySize.height);
// NSLog(@"The width is %d and the height is %d",frame.size.width,frame.size.height);
return frame.size;
}
 
The following seems to work in my experimental project.

Code:
-(NSSize)windowWillResize:(NSWindow*)sender toSize:(NSSize)frameSize
{
	NSSize size = frameSize;

	if ( sender == [self window] )
	{
		NSRect rect = [sender frame];
		size = rect.size;
		size.width = 2 * size.height;
	}

	return size;
}

As stated in the book the request was for height as twice the width but I edited it to match your request as stated.
 
%d is the wrong format specifier. The width and height of NSSize are of type CGFloat, not type int. Use a %e or %f format, or use a type-cast to coerce the CGFloat to an int type:

Code:
NSLog(@"The width is %d and the height is %d", (int)mySize.width, (int)mySize.height);

String Format Specifiers:

https://developer.apple.com/mac/lib...fiers.html#//apple_ref/doc/uid/TP40004265-SW1

You should also think carefully about what it means to return frame.size, where frame is the sending NSWindow's current frame. In particular, think about what the purpose of the frameSize parameter is, and exactly what the delegate method should be doing. If you still don't see a problem, I suggest rereading the reference docs for windowWillResize.
 
by Jove, I've got it!

Here is the correct answer. I don't know why I didn't do this in the 1st place. Brain cramp, I guess.
-(NSSize)windowWillResize:_(NSWindow *)sender
toSize:_(NSSize)frameSize
{
NSSize mySize = frameSize;
mySize = frameSize;
mySize.width = 2 * mySize.height;
NSLog(@"The sent size is %d wide and %d high.",(int)frameSize.width,(int)frameSize.height);
return mySize;
}

Jeff
 
Hi guys,

I just came across this thread.

Is there anything wrong with my version of the code? (It seems to work):

Code:
@implementation DoubleSizeAppDelegate

- (NSSize) windowWillResize: (NSWindow *) sender toSize: (NSSize) frameSize {
	
	NSSize mySize;
	mySize.width = frameSize.width;
	mySize.height = frameSize.width * 2.0;
	NSLog(@"mySize is %f wide and %f tall",mySize.width, mySize.height);
	return mySize;
		
}

By the way, just to check, there was no need to use the * operator for NSSize *mySize because mySize is a C struct and not an NSObject right?

Also, for the code pasted above, assigning mySize = frameSize does a deep copy right? Since the struct is not an NSObject?

Thanks.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.