سؤال

وأنا أتعلم الهدف-C ومحاولة لتطوير تطبيق سحاب بسيطة، ولكنها توقفت عندما الآن، عندما كنت في حاجة إلى إدراج زر في الحوار الخاص بي، وهذا الزر يفتح حوار فتح ملف من شأنها تحديد ملف لضغط ، لكنني لم تستخدم قط حوار فتح ملف، ثم كيف يمكنني فتحه وتخزين الملف المحدد المستخدم في char*؟ شكرا.

وتذكر أن أنا باستخدام GNUstep (لينكس).

هل كانت مفيدة؟

المحلول

في حالة شخص آخر يحتاج هذه الإجابة، ومن هنا:

 int i;
  // Create the File Open Dialog class.
  NSOpenPanel* openDlg = [NSOpenPanel openPanel];

  // Enable the selection of files in the dialog.
  [openDlg setCanChooseFiles:YES];

  // Multiple files not allowed
  [openDlg setAllowsMultipleSelection:NO];

  // Can't select a directory
  [openDlg setCanChooseDirectories:NO];

  // Display the dialog. If the OK button was pressed,
  // process the files.
  if ( [openDlg runModalForDirectory:nil file:nil] == NSOKButton )
  {
   // Get an array containing the full filenames of all
   // files and directories selected.
   NSArray* files = [openDlg filenames];

   // Loop through all the files and process them.
   for( i = 0; i < [files count]; i++ )
   {
   NSString* fileName = [files objectAtIndex:i];
   }

نصائح أخرى

وبفضلVlad إمبالا أنا تحديث إجاباتك للأشخاص الذين يستخدمون OS X v10.6 +

// Create the File Open Dialog class.
NSOpenPanel* openDlg = [NSOpenPanel openPanel];

// Enable the selection of files in the dialog.
[openDlg setCanChooseFiles:YES];

// Multiple files not allowed
[openDlg setAllowsMultipleSelection:NO];

// Can't select a directory
[openDlg setCanChooseDirectories:NO];

// Display the dialog. If the OK button was pressed,
// process the files.
if ( [openDlg runModal] == NSOKButton )
{
    // Get an array containing the full filenames of all
    // files and directories selected.
    NSArray* urls = [openDlg URLs];

    // Loop through all the files and process them.
    for(int i = 0; i < [urls count]; i++ )
    {
        NSString* url = [urls objectAtIndex:i];
        NSLog(@"Url: %@", url);
    }
}

وبالنسبة للأشخاص الذين يستخدمون OS X v10.10 +، استبدل في الأقصى Jangtrakool الجواب:

if ( [openDlg runModal] == NSOKButton )

وقبل

if ( [openDlg runModal] == NSModalResponseOK )
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top