在Cocoa App中展示自定义的对话框

来源:互联网 发布:公司网封端口 编辑:程序博客网 时间:2024/06/04 19:52

继上一篇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:点击打开链接

原创粉丝点击