Changing file types in NSOpenPanel while it's open
16/11/03 10:34
I'm wondering if it is possible to change the file types in an NSOpenPanel while the panel is open...
There is an undocumented (private) instance method in NSSavePanel named _setEnabledFileTypes.
As its name suggests, its allows changing of the file type's list you pass to runModalForTypes.
Here an sample code: we have an accessory view with a matrix of radio buttons (here only two) of which action method is set to doSelectFileTypes. The tag of the selected button cell specifies which file type's array to use:
@implementation MyWindowController
- (IBAction)doSelectFileTypes:(id)sender
{
curTag = [[sender selectedCell] tag] ;
[openPanel _setEnabledFileTypes:(curTag)? ([NSArray arrayWithObjects:@"xls",nil]):([NSArray arrayWithObjects:@"doc",nil])] ;
}
- (IBAction)doOpenAFile:(id)sender;
{
openPanel = [NSOpenPanel openPanel] ;
[openPanel setAccessoryView:ibAcccessoryView] ;
[ibFileTypes selectCellWithTag:0] ;
curTag = 0 ;
[openPanel setCanChooseDirectories:NO] ;
[openPanel setAllowsMultipleSelection:NO];
[openPanel setCanChooseFiles:YES] ;
[openPanel runModalForTypes:[NSArray arrayWithObjects:@"doc",nil]] ;
}
Of course, as usual when using undocumented feature, you should test if the method is implemented before using it for future (and/or past) compatibility.
This code has been tested under Panther only.
As its name suggests, its allows changing of the file type's list you pass to runModalForTypes.
Here an sample code: we have an accessory view with a matrix of radio buttons (here only two) of which action method is set to doSelectFileTypes. The tag of the selected button cell specifies which file type's array to use:
@implementation MyWindowController
- (IBAction)doSelectFileTypes:(id)sender
{
curTag = [[sender selectedCell] tag] ;
[openPanel _setEnabledFileTypes:(curTag)? ([NSArray arrayWithObjects:@"xls",nil]):([NSArray arrayWithObjects:@"doc",nil])] ;
}
- (IBAction)doOpenAFile:(id)sender;
{
openPanel = [NSOpenPanel openPanel] ;
[openPanel setAccessoryView:ibAcccessoryView] ;
[ibFileTypes selectCellWithTag:0] ;
curTag = 0 ;
[openPanel setCanChooseDirectories:NO] ;
[openPanel setAllowsMultipleSelection:NO];
[openPanel setCanChooseFiles:YES] ;
[openPanel runModalForTypes:[NSArray arrayWithObjects:@"doc",nil]] ;
}
Of course, as usual when using undocumented feature, you should test if the method is implemented before using it for future (and/or past) compatibility.
This code has been tested under Panther only.