【问题标题】:Cocoa: How to properly release NSWindowController after fading out its windowCocoa:淡出窗口后如何正确释放 NSWindowController
【发布时间】:2013-09-05 19:48:16
【问题描述】:

我有一个显示启动屏幕的 Cocoa 应用程序。从用户那里收集一些信息后,我检查它是否有效——如果是,我会显示一个绿色的复选标记,然后等待 1 秒钟,然后将启动窗口淡入淡出到我的主应用程序窗口中。我有 2 个带有 2 个窗口 xib 文件的 NSWindowControllers。

在我的startupWindowController 上,我设置了一个按钮插座,可以完成我上面描述的操作。

- (void)fadeOutAndPresentMainWindow {

    // Initialize the main window from XIB
    mainWindowController = [[MyMainWindowController alloc] init];
    NSWindow *mainWindow = [mainWindowController window];

    // Position the main window BEHIND the currently visible startup window
    NSWindow *startupWindow = [startupController window];
    [mainWindow setFrame:[startupWindow frame] display:NO];
    [mainWindow orderWindow:NSWindowBelow relativeTo:[startupWindow windowNumber]];

    // Now wait 1 second and fade out the startupWindow to reveal the main window
    // that is behind it.
    double delayInSeconds = 1.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

        [NSAnimationContext beginGrouping];
        [[NSAnimationContext currentContext] setCompletionHandler:^{

            // releases when closed
            [startupWindow close];

            // deallocates the startup controller *after* the animation? or not...
            [startupWindowController release]; 
            startupWindowController = nil;
        }];

        // Do the fade
        [[startupWindow animator] setAlphaValue:0.0f];
        [NSAnimationContext endGrouping];

        // Now make the main window key
        [mainWindow makeKeyWindow];
    });
}

这一切都很好,但有一个问题:如果用户在淡入淡出动画期间单击带有IBOutlet 的按钮,应用程序将崩溃。 MyStartupController performSelector:withObject:]: message sent to deallocated instance 0x102c00a90。所以问题是startupController 在动画完成之前就被释放了。

所以我想我不确定在淡出后如何正确释放这个窗口控制器。任何想法如何做到这一点?

【问题讨论】:

  • 如果它释放然后保留它,创建一个强指针以在对象周围保持一段时间,直到动画完成。
  • 我在动画完成后释放它。完成块似乎没有在适当的时间被调用。
  • 最好不要使用启动画面...
  • 虽然我同意闪屏被过度使用并且通常没有必要,但它们仍然存在有效的用例。我相信我的应用值得拥有。此外,这个问题在其他不涉及闪屏的用例中完全有效;例如登录屏幕。

标签: objective-c macos cocoa nswindow


【解决方案1】:
Better place to declare the same in your dealloc method:-
- (void)dealloc
{
[startupWindowController release];
startupWindowController=nil;
}

【讨论】:

  • 你初始化windowcontroller实例的类
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多