由于面板是非阻塞的,所以一旦面板打开,代码就会继续执行。打开的面板正在被释放,因为你没有在某个地方引用它。 -openPanel
是一个方便的构造函数,它返回一个autoreleased对象,当当前自动释放池被弹出或者(当在GC应用程序中)下一次运行收集器时它将会消失。在你的情况下,这是你的方法完成后立即。
如果你想面板留下来,你必须明确使用-retain
保留它,并随后-release
它在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
}
如果您使用垃圾收集,保留和释放没有-ops,因此您必须改为存储对NSOpenPanel
的强引用,例如将其存储在实例变量中。