验证 NSOpenPanel 附件视图中的输入

发布于 2024-08-20 06:45:22 字数 494 浏览 1 评论 0原文

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆蝶 2024-08-27 06:45:22

您应该将控制器注册为打开面板的委托,然后实现 -panel:isValidFilename: 委托方法。从该方法返回 NO 可以防止打开的对话框被关闭:

- (BOOL)panel:(id)sender isValidFilename:(NSString *)filename
{
    //validate the field in some way, in this case by making sure it's not empty
    if([[textField stringValue] length] == 0)
    {
        //let the user know they need to do something
        NSAlert *alert = [[NSAlert alloc] init];
        [alert setMessageText:@"Please enter some text."];
        [alert addButtonWithTitle:@"OK"];
        [alert beginSheetModalForWindow:sender modalDelegate:nil didEndSelector:NULL contextInfo:NULL];
        //return NO to prevent the open panel from completing
        return NO;
    }
    //otherwise, allow the open panel to close
    return YES;
}

You should register your controller as the open panel's delegate and then implement the -panel:isValidFilename: delegate method. Returning NO from that method allows you to prevent the open dialog from being closed:

- (BOOL)panel:(id)sender isValidFilename:(NSString *)filename
{
    //validate the field in some way, in this case by making sure it's not empty
    if([[textField stringValue] length] == 0)
    {
        //let the user know they need to do something
        NSAlert *alert = [[NSAlert alloc] init];
        [alert setMessageText:@"Please enter some text."];
        [alert addButtonWithTitle:@"OK"];
        [alert beginSheetModalForWindow:sender modalDelegate:nil didEndSelector:NULL contextInfo:NULL];
        //return NO to prevent the open panel from completing
        return NO;
    }
    //otherwise, allow the open panel to close
    return YES;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文