gpt4 book ai didi

Cocoa:在 NSApp beginSheet 后显示错误导致主窗口隐藏

转载 作者:行者123 更新时间:2023-12-03 16:10:54 26 4
gpt4 key购买 nike

我已将其分解为一个非常小的项目。在应用程序委托(delegate)中使用以下代码:

- (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 上的按钮相关。 (请注意, 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 方法。我故意将这个值留为空,因为我只有很短的时间来编写示例代码。传递实际错误会导致相同的行为。

示例项目已可用 here .

最佳答案

看来您仍在使用旧的 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];

}

关于Cocoa:在 NSApp beginSheet 后显示错误导致主窗口隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10729214/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com