我有一个显示启动屏幕的 Cocoa 应用程序。从用户那里收集一些信息后,我检查它是否有效——如果是,我会显示一个绿色的复选标记,然后等待 1 秒钟,然后将启动窗口淡入淡出到我的主应用程序窗口中。我有 2 个带有 2 个窗口 xib 文件的 NSWindowController。
在我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
在动画完成之前被释放。
所以我想我不确定在淡出后如何正确释放这个窗口控制器。任何想法如何做到这一点?