Selecting and Getting file name of type "XML"

Discussion in 'Mac Programming' started by satyam90, May 9, 2008.

  1. satyam90 macrumors regular

    satyam90

    Joined:
    Jul 30, 2007
    Location:
    Bangalore, India
    #1
    I am using Cocoa with Obj C.
    I am using NSOpenPanel for selecting the file and getting the file name.
    For restricting the files to "XML" I am using the following code:
    Code:
    NSArray* types = [NSArray arrayWithObject:@"xml"];
    	
    NSOpenPanel *oPanel = [NSOpenPanel openPanel];
    	
    [oPanel setAllowsMultipleSelection:NO];
    [oPanel setCanChooseDirectories:NO];
    [oPanel setCanChooseFiles:YES];
    [oPanel setAllowedFileTypes:types];
    
    But still I am able to select other file types too.
    From the hierarchy of NSOpenPanel, its parent is NSSavePanel which is having the method "setAllowedFileTypes". I must be able to use this method as NSOpenPanel will derive the methods of parent.

    Please suggest me some good way to solve the problem.
     
  2. MacDonaldsd macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #2
    When I have done it, I set the types when I run the open panel:

    [oPanel runModalForDirectory:nil file:nil types: types]
     
  3. satyam90 thread starter macrumors regular

    satyam90

    Joined:
    Jul 30, 2007
    Location:
    Bangalore, India
    #3
    I am also using the same above method to open the panel with same parameters. Still I am not able to select only XML files. UI is allowing me to select other format files also.
     
  4. MacDonaldsd macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #4
    The exact code I am using for one of my applications is:
    Code:
    		
    		NSArray *fileTypes = [NSArray arrayWithObjects:@"jpg", @"gif",@"jpeg"
    							  @"png",	@"psd", @"pdf", @"doc",@"rtf",@"xls",@"mp3",@"aac",@"wav",@"aiff",
    							  @"xml",@"key",@"c",@"m",@"mm",@"cpp",nil];
    		
    		
    		NSOpenPanel* oPanel = [NSOpenPanel openPanel];
    		
    		[oPanel setCanChooseDirectories:YES];
    		[oPanel setCanChooseFiles:YES];
    		[oPanel setCanCreateDirectories:NO];
    		[oPanel setAllowsMultipleSelection:YES];
    		[oPanel setAlphaValue:0.95];
    		[oPanel setTitle:@"Select a file to add to documents"];
    		
    		// Display the dialog.  If the OK button was pressed,
    		// process the files.
    		if ( [oPanel runModalForDirectory:nil file:nil types:fileTypes]
    			== NSOKButton ){
    //Do something with files
    }
    
     

Share This Page