How to add a file selector/opener in cocoa with Interface Builder?

20,869

Solution 1

This must be done in Xcode. The code here should work fine.

Just hook the button up with a method using IB and use that example as a guide of what to put in the method.

There's also all sorts of good help WRT NSOpenPanel at Cocoadev, including tips on opening the panel as a sheet instead of a modal window.

Of course you should always read the Apple documentation as well.

Solution 2

I found this page when looking up, how to open up a file open box in Cocoa. With the release of OS X 10.7 a lot of the samples that is linked to is now deprecated. So, here is some sample code that will save you some compiler warnings:

// -----------------
// NSOpenPanel: Displaying a File Open Dialog in OS X 10.7
// -----------------

// Any ole method
- (void)someMethod {
  // Create a File Open Dialog class.
  NSOpenPanel *openDlg = [NSOpenPanel openPanel];

  // Set array of file types 
  NSArray<NSString*> *fileTypesArray = @[@"jpg", @"gif", @"png"];

  // Enable options in the dialog.
  [openDlg setCanChooseFiles:YES];    
  [openDlg setAllowedFileTypes:fileTypesArray];
  [openDlg setAllowsMultipleSelection:YES];

  // Display the dialog box.  If OK is pressed, process the files.
  if ([openDlg runModal] == NSModalResponseOK) {
    // Get list of all files selected
    NSArray<NSURL*> *files = [openDlg URLs];

    // Loop through the files and process them.
    for (NSURL *file in files) {
      // Do something with the filename.
      NSLog(@"File path: %@", [file path]);
    }
  }
}

Solution 3

Interface Builder is for designing and linking together the interface. You want to open files and put them in an array, which is safely on the Xcode side of things. Have the button's action show an NSOpenPanel and give the results to your table's data source.

Share:
20,869
xaddict
Author by

xaddict

I'm a (web) graphic man and developer. php, html, css, js and other frontend languages (and extensions) are my specialty. I also fiddle in Processing, Objective-C and Swift, create Mac and iDevice apps and offline web-apps. I work at a branding and advertising agency

Updated on July 05, 2022

Comments

  • xaddict
    xaddict almost 2 years

    I'm wondering how to make a button or input field in Interface Builder react in such a way that on click it opens a file dialog and lets you select one or more files and puts them into a specified array/table...

    Once the button is pushed and files are chosen (this seems a like quite trivial thing) I guess it will already contain some sort of array (like an array with paths to the selected files) so I've got that covered.. I only need to know how to link the button to a file selector and in what way the file selector delivers the files to me (or paths to the files) so I can redirect them to the array

    Is there an easy way to do this, and more importantly; is there a file selector thingie or do I have to do this with XCode instead of Interface builder?

  • Vibhor Goyal
    Vibhor Goyal about 12 years
    The code link given here uses deprecated APIs. Look at @archieoi Answer for more up to date code.
  • Stefan Kendall
    Stefan Kendall almost 8 years
    Links are not answers.
  • Clearer
    Clearer over 5 years
    The link to Apple's documentation is dead.