验证 NSOpenPanel 附件视图中的输入
我想在 NSOpenPanel 期间向用户征求更多信息,但需要在打开面板完成之前验证该信息。例如,我可能希望允许用户在打开的面板选择中添加有关文件的注释,但需要验证该注释不为空。
我有一个附件视图,其控件绑定到 NSObjectController
,其内容对象又绑定到我用来加载附件视图笔尖的 NSViewController 的表示对象。所表示的对象具有符合NSKeyValueCoding
的验证方法(例如-(BOOL)validateKey:error:
)。当用户修改控件的值时,验证得到正确处理(并通过模式对话框报告违规)。
我的问题是,如果用户没有在附件视图中输入任何内容,我无法弄清楚如何获得验证。例如,假设我在附件视图中有一个文本字段,其绑定对象验证文本长度非零。如果用户输入文本(验证成功),然后删除该文本,验证失败并向用户显示错误。但是,如果用户不输入文本,打开的面板将不会出现错误。在打开的面板关闭之前,如何验证文本是否非零?
I would like to solicit additional information from the user during the NSOpenPanel but need to validate that information before the open panel completes. For example, I may want to allow the user to add a note about the file in the open panel selection but need to validate that that comment is not empty.
I have an accessory view whose controls are bound to an NSObjectController
whose content object in turn is bound to the represented object of the NSViewController that I use to load the accessory view's nib. The represented object has NSKeyValueCoding
-compliant validation methods (e.g. -(BOOL)validateKey:error:
). Validation is correctly handled (and violations reported via modal dialog) when the user modifies the values of the controls.
My problem is that I can't figure out how to get validation if the user doesn't enter anything in the accessory view. For example, say I have a single text field in the accessory view whose bound object validates that the text is non-zero in length. If the user enters text (validation succeeds), then deletes the text, validation fails and the user is presented with the error. However, if the user doesn't enter text, the open panel dismisses without error. How can I validate that the text is non-zero before the open panel dismisses?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该将控制器注册为打开面板的委托,然后实现
-panel:isValidFilename:
委托方法。从该方法返回NO
可以防止打开的对话框被关闭:You should register your controller as the open panel's delegate and then implement the
-panel:isValidFilename:
delegate method. ReturningNO
from that method allows you to prevent the open dialog from being closed: