继上一篇Toolbar的使用,我想在点击工具栏item的时候,下拉一个对话框。
官方文档见此:点击打开链接
通过创建alert:
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert addButtonWithTitle:@"OK"];
[alert addButtonWithTitle:@"Cancel"];
[alert setMessageText:@"Delete the record?"];
[alert setInformativeText:@"Deleted records cannot be restored."];
[alert setAlertStyle:NSWarningAlertStyle];
并展现模态对话框:
[alert beginSheetModalForWindow:[searchField window] modalDelegate:self didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) contextInfo:nil];
可以很好地工作:对话框会从App顶部slide down。

但是当我新建一个自带nib文件的windowController时,对话框却不从App顶部滑下来,而是直接出现在屏幕中的另一块位置,与main window看起来没什么直接联系。
- (void)simpleToolbarItemDidClick:(id)sender
{
[NSApp beginSheet:self.simpleSheetCtrl.window
modalForWindow:self.window
modalDelegate:self.simpleSheetCtrl
didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:)
contextInfo:NULL];
}
最后,在SO上下载了一个demo来看,一一对比,才发现nib文件的visible at launch选项没有去掉。
这里有一份相关的gotcha:点击打开链接
本文介绍了如何在Cocoa App中展示自定义对话框,特别是在点击工具栏item时弹出对话框的问题。作者遇到对话框不从App顶部滑下,而是直接在屏幕中央显示的问题。通过查看官方文档和解决一个关于nib文件'visible at launch'选项的gotcha,成功解决了这个问题。
1095

被折叠的 条评论
为什么被折叠?



