Alex Rivera | Logout

NSOpenPanel - Set file type?

Asked 2010-11-27T01:01:10.713
10

Just looking on what I would use to only allow specific files to be selected (Images for now)

setFileTypesArray returns

NSOpenPanel may not respond to -setFileTypesArray:

and then the panel doesn't open up at all. Heres my code:

    NSArray  * fileTypes = [NSArray arrayWithObjects:@"png",@"tiff",@"baz",nil];

NSLog(@"Button Pressed");
[textField setStringValue:@"Test"];
int i; // Loop counter.

NSOpenPanel* openDlg = [NSOpenPanel openPanel];

[openDlg setCanChooseFiles:YES];
[openDlg setFileTypesArray:fileTypes];

Thanks.

Edit
Report

1 Answer

13

You're looking for a delegate method from NSSaveOpenPanel's delegate

-(BOOL)panel:(id)sender shouldShowFilename:(NSString *)filename
{
        NSString* ext = [filename pathExtension];
        if (ext == @"" || ext == @"/" || ext == nil || ext == NULL || [ext length] < 1) {
                return TRUE;
        }

        NSLog(@"Ext: '%@'", ext);

        NSEnumerator* tagEnumerator = [[NSArray arrayWithObjects:@"png", @"tiff", @"jpg", @"gif", @"jpeg", nil] objectEnumerator];
        NSString* allowedExt;
        while ((allowedExt = [tagEnumerator nextObject]))
        {
                if ([ext caseInsensitiveCompare:allowedExt] == NSOrderedSame)
                {
                        return TRUE;
                }
        }

        return FALSE;
}

Then, set your panel's delegate to "self", or wherever your define this method above.

answered 2010-11-27T01:49:36.087

Your Answer