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.
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.
-openPanelis 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-releaseit in the didEndSelector: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.