5

私はこれを非常に小さなプロジェクトに分解しました。アプリケーションデリゲートで次のコードを使用します。

- (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のままにしました。実際のエラーを渡すと、同じ動作になります。

サンプルプロジェクトはこちらから入手できます。

4

2 に答える 2

5

まだ古い API を使用しているようです。新しい API を試してください。

(UserLoginWindowController ウィンドウの [起動時に常に表示] の選択を解除します)

- (IBAction)userButtonPressed:(id)sender {

    UserLoginWindowController * wc = [UserLoginWindowController new];
    // we keep a reference, so the WC doesn't deallocate
    self.modalWindowController = wc;

    [[self window] beginSheet:[wc window] completionHandler:^(NSModalResponse returnCode) {
        self.modalWindowController = nil;
    }];

}

UserLoginWindowController で

- (IBAction)cancelButtonPressed:(id)sender {

    [[[self window] sheetParent] endSheet:[self window] returnCode:NSModalResponseCancel];

}
于 2014-09-16T13:51:03.513 に答える
1

ウィンドウを開くために、beginSheet:.....とrunModalForWindow:の2つのメソッドを使用しています。必要なのはそのうちの1つだけです。ウィンドウにシートを貼り付ける場合は、最初の方法を使用します。スタンドアロンウィンドウが必要な場合は、2番目の方法を使用します。同様に、onCloseメソッドでは、シートを閉じる場合はendSheet:returnCode:を使用する必要があり(そのメソッドの引数はself.windowではなくtestingWindowController.windowである必要があります)、モーダルウィンドウを閉じる場合はstopModalWithCode:を使用する必要があります。両方を持つべきではありません。

于 2012-05-24T00:37:49.220 に答える