所以,假设我有一个这样的方法,用于在实际关闭之前检查document
是否已被修改:
- (BOOL) canCloseDocument:(id)document
{
if ([document modified])
CONFIRM_ALERT(@"Close Document",
@"Are you sure you want to close this document?",
[[core browser] window],
@selector(confirm:code:context:),
nil);
else
return YES;
}
在这种情况下,confirm:code:context:
方法将被调用,而canCloseDocument
将返回NOTHING。
以下是我的CONFIRM_ALERT
定义:
#define CONFIRM_ALERT(X,Y,Z,W,V) \
NSAlert* confirmAlert = [NSAlert alertWithMessageText:X \
defaultButton:@"OK" \
alternateButton:@"Cancel" \
otherButton:nil \
informativeTextWithFormat:Y]; \
[confirmAlert beginSheetModalForWindow:Z \
modalDelegate:self \
didEndSelector:W \
contextInfo:V];
问题:
我怎么能这样做以便显示警告表,并且在同一方法(canCloseDocument:
)内检索值(按下确定?取消按??),以便它可以返回{{1 }或YES
?
答案 0 :(得分:1)
表单是窗口模式,而不是应用程序模式。这意味着他们没有按照你希望的方式运作。工作表会显示,但执行流程必须返回主事件循环,以便用户可以继续在应用程序的其他窗口上操作。
如果您在返回之前想要答案,则必须使用模态警报。创建警报,然后在其上调用-runModal
,而不是-beginSheetModalForWindow:modalDelegate:didEndSelector:contextInfo:
。
但是,这会阻止用户对您的应用执行任何其他操作,直到他们解除模态警报。请注意,这不仅仅是模态警报中固有的,它是您希望-canCloseDocument:
在得到答案之前不返回的固有内容。这意味着执行流程不会返回到主事件循环,这是允许与您的应用程序进行交互的原因。