Question

I have an issue with an NSPanel acting strangely and have created a Sample App to demonstrate this behaviour.

The App was generated from Xcode 4's template and simply creates a panel and then opens and closes it based on button presses:

enter image description here

The strange behaviour I have observed:

  1. Under Lion, after opening the panel for the first time, the panel follows the main window around, which is correct behaviour. However after closing it and then re-opening it no longer follows the main window around.
  2. Under Snow Leopard, when closing the panel the main window is also closed!

EDIT: Just to be clear; the behaviour I expect is for the panel to follow the main window around when the main window is moved; and for that to be true after the panel is closed and subsequently re-opened. Also I expected the panel and main window to behave the same way under Snow Leopard and Lion.

The important part of the code is here:

@implementation MyAppDelegate

- (void)dealloc
{
    [_panel release];
    [super dealloc];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    _panel = [[NSPanel alloc]
             initWithContentRect:NSMakeRect(400, 400, 200, 100)
             styleMask:NSUtilityWindowMask|NSClosableWindowMask|NSTitledWindowMask
             backing:NSBackingStoreBuffered
             defer:YES];
    [_panel setTitle:@"A Panel"];
}

- (IBAction)openPanel:(id)sender
{
    [_panel setParentWindow:[self window]];
    [_panel makeKeyAndOrderFront:sender];
}

- (IBAction)closePanel:(id)sender
{
    [_panel orderOut:sender];
}

@end

Note: I added the unnecessary setParentWindow call to the openPanel method to assert that the parent window is reset every time the panel was opened; it made no difference however.

Can someone please explain what I'm doing wrong?

EDIT: There is some confusion about the missing @synthesize window = _window from the implementation file, but I have just opened another project sample that I created to test for memory leak behaviour and it's not in there either. I am using Xcode 4.4, so it's possibly a bug in that, however I don't think the project templates have changed.

Was it helpful?

Solution

You're not supposed to set the parent-child relationship from the child, but from the parent. The setParentWindow: docs say:

This method should be called from a subclass when it is overridden by a subclass’s implementation. It should not be called otherwise.

Instead, use addChildWindow:ordered:, like so:

- (IBAction)openPanel:(id)sender
{
    [[self window] addChildWindow:_panel ordered:NSWindowAbove];
    //[_panel setParentWindow:[self window]];
    [_panel makeKeyAndOrderFront:sender];
}

I didn't test this under Snow Leopard, but it fixes the behavior for me when run on Lion.

Rob Keniger notes below that on Snow Leopard you should also do [[self window] removeChildWindow:_panel] before ordering the panel out. (I assume this is also a good idea on Lion.)

OTHER TIPS

I'm a little confused about your question. I downloaded and checked out your project. First, somehow you're missing the @synthesize command for the application's main window. You said you used the template but somehow it's missing. Since the AppDelegate header file has a @property for the window, you need an @synthesize command in the implementation file for it. I'm not sure how you lost that line in your project but add this just after the @implementation line...

@synthesize window = _window;

Second, why do you state the following?

Under Lion, after opening the panel for the first time, the panel follows the main window around, which is correct behavior.

What you're saying makes no sense. When you create your panel you use "initWithContentRect:NSMakeRect(400, 400, 200, 100)". Notice that you create it at position (400, 400) with a size of (200, 100). So the first time it opens it opens at the screen location (400, 400). It has nothing to do with the position of the main window.

Anyway, after adding the @synthesize part I could compile and run the application with no errors. It's working as expected in 10.7 for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top