views:

94

answers:

1

Hi,

I am using this code:

    NSOpenPanel *openPanel = [NSOpenPanel openPanel];
    [openPanel beginForDirectory:nil file:nil types:[NSImage imageFileTypes] modelessDelegate:self didEndSelector:NULL contextInfo:NULL];

This is the only code in the method. When the method is called, the open panel appears on-screen for a second then disappears. How do I prevent this?

Thanks.

A: 

Since the panel is non-blocking, code execution continues once the panel has opened. The open panel is being deallocated because you are not holding a reference to it somewhere. -openPanel is a convenience constructor and returns an autoreleased object which will go away when the current autorelease pool is popped or (in a GC app) when the collector is next run. In your case, this is as soon as your method has finished.

If you want the panel to stick around, you must specifically retain it using -retain, and then subsequently -release it in the didEndSelector:

- (void)showPanel
{
    NSOpenPanel *openPanel = [[NSOpenPanel openPanel] retain]; //note the retain
    [openPanel beginForDirectory:nil 
                            file:nil 
                           types:[NSImage imageFileTypes] 
                modelessDelegate:self 
                  didEndSelector:@selector(myOpenPanelDidEnd:returnCode:contextInfo:)
                     contextInfo:NULL];
}

- (void)myOpenPanelDidEnd:(NSOpenPanel *)panel returnCode:(int)returnCode contextInfo:(void*)contextInfo
{
    NSArray* fileNames = [panel filenames];
    [panel release];
    //do something with fileNames
}

If you're using Garbage Collection, retain and release are no-ops, so you must instead store a strong reference to the NSOpenPanel, such as storing it in an instance variable.

Rob Keniger