Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

satyam90

macrumors regular
Original poster
Jul 30, 2007
242
0
Bangalore, India
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.
 
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.

When I have done it, I set the types when I run the open panel:

[oPanel runModalForDirectory:nil file:nil types: types]
 
When I have done it, I set the types when I run the open panel:

[oPanel runModalForDirectory:nil file:nil types: types]

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.
 
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
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.