Cocoa:如何在淡出窗口后正确释放NSWindowController

时间:2013-09-05 19:48:16

标签: objective-c macos cocoa nswindow

我有一个Cocoa应用程序,它提供了一个启动屏幕。从用户收集一些信息后,我检查并查看它是否有效 - 如果是,我显示绿色复选标记,然后等待1秒,然后将启动窗口交叉淡入我的主应用程序窗口。我有2个NSWindowControllers和2个Window xib文件。

在我的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被解除分配。

所以我想我不确定如何在淡出它之后正确释放这个窗口控制器。任何想法如何实现这一目标?

1 个答案:

答案 0 :(得分:0)

Better place to declare the same in your dealloc method:-
- (void)dealloc
{
[startupWindowController release];
startupWindowController=nil;
}