私はこれを非常に小さなプロジェクトに分解しました。アプリケーションデリゲートで次のコードを使用します。
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
TestingWindowController * testingWindowController = [[TestingWindowController alloc] initWithWindowNibName: @"TestingWindowController"];
// Begin our sheet
[NSApp beginSheet: testingWindowController.window
modalForWindow: self.window
modalDelegate: self
didEndSelector: @selector(windowDidEnd:returnCode:contextInfo:)
contextInfo: NULL];
}
- (void)windowDidEnd:(id)alert returnCode:(NSInteger)returnCode contextInfo:(id) contextInfo
{
// If the user did not accept, then we really don't care what else they did!
if (returnCode != NSOKButton) return;
// We have had an error. Display it.
[[NSApplication sharedApplication] presentError: nil
modalForWindow: self.window
delegate: nil
didPresentSelector: nil
contextInfo: NULL];
}
そして、次のアクションは、ウィンドウペン先のボタンに関連付けられています。(ペン先のウィンドウも起動時に表示されないように設定されていることに注意してください)。
- (IBAction) onClose: (id) sender
{
[[NSApplication sharedApplication] endSheet: self.window
returnCode: NSOKButton];
[self.window orderOut: nil];
} // End of onClose
実行するとonClose
、すべてのウィンドウが消え、エラーダイアログだけが残ります(メインウィンドウが消えました)。
私のコードに何か問題がありますか?メインウィンドウが消えるのはなぜですか?
注:presentErrorメソッドにエラーを渡していないことはわかっています。サンプルコードを書く時間が短かったので、意図的にこれをnullのままにしました。実際のエラーを渡すと、同じ動作になります。
サンプルプロジェクトはこちらから入手できます。