如何从单独的NIB显示工作表 [英] How to show sheet from separate NIB
问题描述
如何在一个单独的NIB中放置一个窗口,给它自己的NSWindowController,使它滑动成一个表?
这是一个典型的东西)
我试图在我的主窗口中显示一个自定义工作表(从父窗口的标题栏向下滑动的窗口)。我想做的是标准的,我想,但我找不到清楚的例子或解释如何做我想要的。
我试图do:
- 我的应用程式委托拥有主视窗,其中有一个按钮可开启「设定」工作表。
- 设置工作表:
- 位于单独的NIB中。
- 当用户点击设置时,我将尝试使用Apple的示例代码[1]。
- (void)showCustomSheet:(NSWindow *)window
//用户要求查看自定义显示。显示它。
{
if(!settingsSheet)
//检查settingsSheet实例变量,以确保自定义工作表不存在。
[NSBundle loadNibNamed:@SettingsSheetowner:self];
//但是这样做使设置表NOT nil?
[NSApp beginSheet:settingsSheet
modalForWindow:window
modalDelegate:self
didEndSelector:@selector(didEndSheet:returnCode:contextInfo :)
contextInfo:nil ];
//表格在这里。
//返回事件循环的处理
}
请原谅以下简单而无数的问题:
- 当我呼叫时,
loadNibName:owner:
,我不希望owner
为self
,因为这使我的应用程序委托的 MyCustomSheet - 这是我的SettingsWindowsController
应该是。但是,我不知道如何让SettingsWindowsController
这个方法中的所有者。 - 如果我的工作表有Visible at launch ,
loadNibName:owner:
立即将窗口显示为正常窗口,而不是从主窗口中滑出的工作表。 - 如果我的工作表没有选中Visible at launch,则
beginSheet:modalForWindow:etc
会导致模态会话需要模态窗口。我很确定这是因为我使Nib的所有者self
(如我已经提到的)。 - 在示例代码中,我不知道名为@SettingsSheet的Nib如何与实例变量
settingsSheet
关联 - 但它们显然是 相关,因为代码检查第一:if(!settingsSheet)
(我已经标记了评论//但是这样做设置表NOT nil?感谢您耐心阅读所有这一切!
h2_lin>解决方案-
创建
SettingsWindowController
的实例,使用initWithWindowNibName:
-
请参阅1
-
您的实例变量将可用于
SettingsWindowController
How do I put a window in a separate NIB, give it its own NSWindowController, make it slide out as a sheet?
(Is this a typical thing to do with sheets?)
I am trying to show a custom sheet (a window that slides down from the title bar of the parent window) from my main window. What I'm trying to do is standard, I think, but I cannot find clear examples or explanations for how to do exactly what I want.
What I am trying to do:
- My app delegate owns the main window, which has a button to open a "settings" sheet.
- The "settings" sheet:
- is in a separate NIB.
- has file owner set to class SettingsWindowController, which is subclass of NSWindowsController
- When user clicks "settings", I am trying to use Apple's [sample code][1]
- (void)showCustomSheet: (NSWindow *)window // User has asked to see the custom display. Display it. { if (!settingsSheet) //Check the settingsSheet instance variable to make sure the custom sheet does not already exist. [NSBundle loadNibNamed:@"SettingsSheet" owner: self]; //BUT HOW DOES THIS MAKE settingsSheet NOT nil? [NSApp beginSheet: settingsSheet modalForWindow: window modalDelegate: self didEndSelector: @selector(didEndSheet:returnCode:contextInfo:) contextInfo: nil]; // Sheet is up here. // Return processing to the event loop }
Please excuse the following simplistic and numerous questions:
- When I call,
loadNibName:owner:
, I don't wantowner
to beself
, because that makes my app delegate the owner of the "MyCustomSheet" - that's what mySettingsWindowsController
is supposed to be for. However, I don't know how makeSettingsWindowsController
the owner in this method. - If my sheet has "Visible at launch" checked, then
loadNibName:owner:
immediately displays the window as a normal window, not as a sheet that slides out from the main window. - If my sheet has "Visible at launch" not checked, then
beginSheet:modalForWindow:etc
causes "Modal session requires modal window". I'm pretty sure this is because I made the Nib's ownerself
(as I mentioned already). - In the sample code, I don't know how the Nib named @"SettingsSheet" is "associated" with the instance variable
settingsSheet
- but they apparently are related because the code checks first:if (!settingsSheet)
(I've marked this with comment//BUT HOW DOES THIS MAKE settingsSheet NOT nil?
)
Thanks for your patience in reading all this!
解决方案Create an instance of
SettingsWindowController
, useinitWithWindowNibName:
You don't want it visible at launch.
See 1.
Your instance variables will be available to
SettingsWindowController
这篇关于如何从单独的NIB显示工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-