setNameFieldStringValue in ModalDialog

I have an NSSavePanel with a custom accessoryView in my Mac OS application. The accessory view is a popup menu with a list of file types that the user can use to save the file. I can initialize file name to be saved with a default .type when I am initializing the NSSavePanel, using [savePanel setNameFieldStringValue: (NSString*)fileName]; but not when I run the panel with [savePanel runModal]. The IBAction that is called when the user selects a new file extension creates a file name with the new file type extension but when the IBAction invokes the [savePanel setNameFieldStringValue: (NSString*)newFileName]; the name isn't updated in the panel. The savePanel and its nameFieldString don't respond to the setNeedsDisplay method. This needs to work so that the user can see what type of file he is saving to and also the savePanelDidEnd method uses this name to create a URL for saving the file with the file name and extension. There is a delegate method for the NSOpenSavePanelDelegate, panelSelectionDidChange. If I implement this as a delegate method will it work?

The thin and very old developer documentation describes how to attach an accessory panel but not how to make it change the fileName.
Eventually with some difficulty I solved this problem.

To make sure that the accessory panel is loaded from the .xib I do this:
IBOutlet NSView* accessoryView;
if(!accessoryView){ //not loaded yet
        NSArray *_Nullable* topLevelObjects = nil; //allocated as nil but doing it explicitly supresses a compiler warning
        if([[NSBundle mainBundle] loadNibNamed: @"accessoryView" owner: self topLevelObjects: topLevelObjects] == NO){
            myCustomAlert(@"INTERNAL ERROR!", @"Unable to load accessory popup menu xib in Save as dialog.");
            return;
        }
    }

I encountered a strange error. If I provide the NSSavePanel with a list of fileTypes like this:
NSArray * fileTypes = [NSArray arrayWithObjects: @".tiff", @".pdf", @".jpg", @".png", @".jp2", @".bmp", @".insc", nil];
[savePanel setAllowedFileTypes: fileTypes];
The savePanel will encounter an error when I run it with [NSSavePanel runModal];

When the user selects a different file type from the accessoryView popup menu in the NSSavePanel the method that is executed just does this:
[savePanel setAllowedFileTypes: [NSArray arrayWithObject: [[[sender selectedItem] title] lowercaseString]]];
This changes the file extension when the panel is running modal.
setNameFieldStringValue in ModalDialog
 
 
Q