NSSavePanel:压缩“确认替换?"按钮.对话 [英] NSSavePanel: Squelching the "confirm replace?" dialog
问题描述
在Nav Services的世界中,可以指定kNavDontConfirmReplacement
作为创建NavDialogRef
的选项,当使用已存在的文件名进行保存时,它不会要求用户确认文件的替换.如何指定可可NSSavePanel
的等效行为?
In the Nav Services world one could specify kNavDontConfirmReplacement
as an option to create a NavDialogRef
that would not ask the user to confirm the replacement of a file when saving with a file name that already exists. How do I specify an equivalent behavior with the Cocoa NSSavePanel
?
推荐答案
以下是操作方法:
- 添加委托以处理NSSavePanel回调
- 在您的代表中覆盖
- (NSString*)panel:(id)sender userEnteredFilename:(NSString*)filename confirmed:(BOOL)okFlag
- 在代表中:
- Add a delegate to handle NSSavePanel callbacks
- Override
- (NSString*)panel:(id)sender userEnteredFilename:(NSString*)filename confirmed:(BOOL)okFlag
in your delegate - In the delegate:
- 如果
okFlag
是false
,则返回filename
- 否则,请在您的代表中将
filename
保留为NSString*
- 返回一些不太可能是实际文件名的唯一字符串
- If
okFlag
isfalse
, returnfilename
- Otherwise, retain
filename
as anNSString*
in your delegate - Return some unique string that is highly unlikely to be the name of an actual file
NSSavePanel
返回代码时,请从委托方法中提取filename的值,并丢弃NSSavePanel
告诉您的任何文件名(应为唯一字符串).
NSSavePanel
returns to your code, pull the value of filename from your delegate method, and discard whatever filename NSSavePanel
told you (which should be your unique string).由于操作系统在进行确认替换检查之前已调用userEnteredFilename:
,因此您有机会获得用户指定的内容,而无需让操作系统进入机密状态.唯一的字符串将确保不会意外弹出确认替换对话框.
Since userEnteredFilename:
is called by the OS before the confirm-replace check is made it gives you a chance to get what the user specified without letting the OS in on the secret. The unique string will assure that the confirm-replace dialog is not popped accidentally.
但有效.
这篇关于NSSavePanel:压缩“确认替换?"按钮.对话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!