Cocoa:在NSApp beginSheet结果隐藏主窗口后显示错误

时间:2012-05-23 23:21:32

标签: cocoa nswindow cocoa-sheet

我把它分解成一个非常小的项目。在应用程序委托中使用以下代码:

- (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];
}

以下操作与Windows笔尖上的按钮相关联。 (请注意,nib的窗口也设置为在启动时不可见。)

- (IBAction) onClose: (id) sender
{
    [[NSApplication sharedApplication] endSheet: self.window
                                     returnCode: NSOKButton];

    [self.window orderOut: nil];    
} // End of onClose

最终发生的事情是,一旦onClose运行,所有窗口都会消失,除了错误对话框(主窗口消失了)之外我什么都没有。 Error dialog with no main window

我的代码有问题吗?为什么我的主窗口会消失?

注意:我知道我没有将错误传递给presentError方法。我特意留下这个null,因为我只有很短的时间来编写示例代码。传递实际错误会导致相同的行为。

示例项目可用here

2 个答案:

答案 0 :(得分:5)

看起来你还在使用旧的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];

}

答案 1 :(得分:1)

您正在使用2种方法打开窗口,beginSheet:.....和runModalForWindow:。你只需要其中一个。如果要将窗体附加到窗口,请使用第一种方法,如果需要独立窗口,请使用第二种方法。同样,在你的onClose方法中,你应该使用endSheet:returnCode:如果你关闭一个工作表(该方法的参数应该是testingWindowController.window而不是self.window),并且stopModalWithCode:如果你关闭了一个模态窗口,你不应该同时拥有它们。

相关问题