Verifying input to an NSOpenPanel accessory

I would like to receive additional information from the user during NSOpenPanel, but you need to check this information before completing the open panel. For example, I can let the user add a note about a file to an open panel, but I need to check that this comment is not empty.

I have an accessory whose controls are bound to NSObjectController, the content object of which, in turn, is bound to the presented NSViewController object, which I use to load the auxiliary nib view. The presented object has NSKeyValueCoding-complex verification methods (for example, -(BOOL)validateKey:error:). Validation is correctly processed (and violations are reported through a modal dialog) when the user changes the values ​​of the controls.

My problem is that I can’t understand how to get confirmation if the user does not enter anything into the accessory. For example, let's say that I have one text field in an accessory whose related object checks that the text is not equal to zero in length. If the user enters text (verification completes successfully), then deletes the text, verification fails and the user is presented with an error. However, if the user does not enter text, the open panel is rejected without errors. How to check that the text is not equal to zero before the open panel is rejected?

+3
source share
1 answer

, -panel:isValidFilename:. NO :

- (BOOL)panel:(id)sender isValidFilename:(NSString *)filename
{
    //validate the field in some way, in this case by making sure it 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;
}
+3

Source: https://habr.com/ru/post/1731462/


All Articles