打开面板出现然后立即消失
我正在使用这段代码:
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel beginForDirectory:nil file:nil types:[NSImage imageFileTypes] modelessDelegate:self didEndSelector:NULL contextInfo:NULL];
这是该方法中的唯一代码。调用该方法时,打开的面板会在屏幕上出现一秒钟,然后消失。我该如何防止这种情况?
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于面板是非阻塞的,因此一旦面板打开,代码就会继续执行。打开的面板正在被释放,因为您没有在某处保存对它的引用。
-openPanel
是一个方便的构造函数,它返回一个自动释放的对象,当当前自动释放池被弹出或(在 GC 应用程序中)下次运行收集器时,该对象将消失。就您而言,这是您的方法完成后立即执行的操作。如果您希望面板保留下来,则必须使用
-retain
专门保留它,然后随后在 didEndSelector 中-release
它:如果您使用垃圾收集, keep 和release 是无操作的,因此您必须存储对
NSOpenPanel
的强引用,例如将其存储在实例变量中。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: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.