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.