Kyl*_*yle 5 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];
}
Run Code Online (Sandbox Code Playgroud)
以下操作绑定到Windows笔尖上的按钮.(请注意,nib的窗口也设置为在启动时不可见).
- (IBAction) onClose: (id) sender
{
[[NSApplication sharedApplication] endSheet: self.window
returnCode: NSOKButton];
[self.window orderOut: nil];
} // End of onClose
Run Code Online (Sandbox Code Playgroud)
最终发生的事情是,一旦我onClose运行,所有的窗口都会消失,除了错误对话框(主窗口已经消失)之外我什么都没有.
我的代码有问题吗?为什么我的主窗口会消失?
注意:我知道我没有将错误传递给presentError方法.我特意留下这个null,因为我只有很短的时间来编写示例代码.传递实际错误会导致相同的行为.
示例项目可在此处获得.
看起来你还在使用旧的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;
}];
}
Run Code Online (Sandbox Code Playgroud)
在UserLoginWindowController中
- (IBAction)cancelButtonPressed:(id)sender {
[[[self window] sheetParent] endSheet:[self window] returnCode:NSModalResponseCancel];
}
Run Code Online (Sandbox Code Playgroud)
您正在使用两种方法打开窗口,beginSheet:..... 和 runModalForWindow:。你只需要其中之一。如果您想要将一张纸附加到窗口上,请使用第一种方法,如果您想要一个独立的窗口,请使用第二种方法。同样,在 onClose 方法中,如果要关闭工作表,则应该使用 endSheet:returnCode: (该方法的参数应该是testingWindowController.window 而不是 self.window),如果要关闭模态窗口,则应该使用 stopModalWithCode:你不应该两者兼得。