打开面板出现然后立即消失

发布于 2024-08-20 13:24:37 字数 307 浏览 6 评论 0原文

我正在使用这段代码:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凡间太子 2024-08-27 13:24:37

由于面板是非阻塞的,因此一旦面板打开,代码就会继续执行。打开的面板正在被释放,因为您没有在某处保存对它的引用。 -openPanel 是一个方便的构造函数,它返回一个自动释放的对象,当当前自动释放池被弹出或(在 GC 应用程序中)下次运行收集器时,该对象将消失。就您而言,这是您的方法完成后立即执行的操作。

如果您希望面板保留下来,则必须使用 -retain 专门保留它,然后随后在 didEndSelector 中 -release 它:

- (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
}

如果您使用垃圾收集, 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:

- (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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文