Question

I have an NSOpenPanel with an accessory view. The accessory view is simple - it's a single checkbox that when checked, allows the user to select any file; when un-checked, requires the file to be one in the list of supported extensions.

NSOpenPanel initialization and display:

NSOpenPanel* dialog = [NSOpenPanel openPanel];

[dialog setAllowedFileTypes:allowedFileTypes];
[dialog setAccessoryView:openPanelAccessoryView];

openPanel = dialog;

[dialog beginSheetModalForWindow:[self activeWindow]
               completionHandler:^(NSInteger result)
 {
     ...
 }];

IBAction for the checkbox:

- (void)openUnrecognizedFiles:(id)sender
{
    if ([sender state])
        [openPanel setAllowedFileTypes:nil];
    else
        [openPanel setAllowedFileTypes:@[@"dsk"]];
}

According to the documentation, one can use setAllowedFileTypes while the panel is displayed:

The allowed file types can be changed while the panel is running (for example, from an accessory view).

However, this does not seem work as expected: the current view does not reload - as you scroll, files further down do get enabled/disabled as appropriate for the new settings; however, files originally visible are unaffected.

I need some way to refresh the contents of the current directory when the user toggles the accessory view checkbox - however, I can't seem to find any way to do it. Any suggestions?

EDIT, Oct. 15, 2013: This seems to be caused by a bug in Mavericks more than anything else. Same code running on Mountain Lion works without any issues, just like the two commenters here noted.

Was it helpful?

Solution

I have tried like this and it worked:

NSOpenPanel* dialog = [NSOpenPanel openPanel];

    [dialog setAllowedFileTypes:[NSArray arrayWithObject:@"png"]];

    NSButton *openPanelAccessoryView = [[[NSButton alloc] initWithFrame:NSMakeRect(0.0, 0.0, 324.0, 22.0)] autorelease];

    [openPanelAccessoryView setButtonType:NSSwitchButton];

    [openPanelAccessoryView setBezelStyle:0];

    [openPanelAccessoryView setAction:@selector(openUnrecognizedFiles:)];

    [openPanelAccessoryView setTarget:self];

    [dialog setAccessoryView:openPanelAccessoryView];

     openPanel = dialog;

    [dialog beginSheetModalForWindow:[[self view] window] completionHandler:^(NSInteger result){
        if(result == NSFileHandlingPanelOKButton)
        {

        }
    } ];

This is action same as yours:

- (void)openUnrecognizedFiles:(id)sender

{
     if ([sender state])
      [openPanel setAllowedFileTypes:nil];
     else
      [openPanel setAllowedFileTypes:[NSArray arrayWithObject:@"jpeg"]];
}

Now file type is changing as per check box on & off.

OTHER TIPS

You can not hide files using setAllowedFileTypes: method. This method will only enable/disable files in open panel.

From NSSavePanel.h

This property will determine which files should be enabled in the open panel

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top