2011-03-16 10 views
7

वर्तमान में जो एक फ़ाइल को खोलने के लिए अनुमति देते मेरे ऐप inteface पर एक बटन है, यहाँ मेरी खुला कोड है:कोको/Obj सी - ओपन फ़ाइल जब आवेदन आइकन पर खींचकर

मेरी app.h में:

- (IBAction)selectFile:(id)sender; 

मेरी app.m में:

@synthesize window; 

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { 

} 

- (IBAction)selectFile:(id)sender { 

    NSOpenPanel *openPanel = [NSOpenPanel openPanel]; 
    NSArray *fileTypes = [NSArray arrayWithObjects:@"xml",nil]; 

    NSInteger result = [openPanel runModalForDirectory:NSHomeDirectory() file:nil types:fileTypes ]; 

    if(result == NSOKButton){ 

     NSString * input = [openPanel filename]; 

मैं आवेदन आइकन खींचें & गिरावट के साथ खुलने के अनुमति देने के लिए मेरे कोड का संपादन कैसे कर सकते हैं?
नोट: मैंने .plist फ़ाइल संपादित की और "xml" के लिए एक पंक्ति जोड़ा लेकिन यह कुछ भी बदल गया, जब मेरी फ़ाइल आइकन पर गिरा दी गई तो एक त्रुटि आई। मेरे अनुप्रयोग है आपकी मदद के लिए नहीं एक दस्तावेज आधारित अनुप्रयोग


धन्यवाद: जो मेरी कोड
नोट 3 को देखें: -:
नोट 2 selectFile को मैं "> ओपन ... फ़ाइल" लिंक किए गए !
मिस्किया

+0

आप NSDocument का उपयोग क्यों नहीं करते? पर्याप्त लचीला नहीं है? –

उत्तर

15

पहले .plist फ़ाइल के अंदर CFBundleDocumentTypes में उचित एक्सटेंशन जोड़ें। OpenFile: (एक फ़ाइल गिरा)
- आवेदन: openFiles: (एक से अधिक फ़ाइलों गिरा)

संदर्भ:
NSApplicationDelegate Protocol Reference

- आवेदन
:

अगला निम्नलिखित प्रतिनिधियों को लागू टिप्पणी का जवाब:

चरण-दर-चरण परीक्षा मिसाल, उम्मीद है कि यह बनाता है सब कुछ स्पष्ट :)

.plist फ़ाइल में जोड़ें:

<key>CFBundleDocumentTypes</key> 
     <array> 
      <dict> 
       <key>CFBundleTypeExtensions</key> 
       <array> 
        <string>xml</string> 
       </array> 
       <key>CFBundleTypeIconFile</key> 
       <string>application.icns</string> 
       <key>CFBundleTypeMIMETypes</key> 
       <array> 
        <string>text/xml</string> 
       </array> 
       <key>CFBundleTypeName</key> 
       <string>XML File</string> 
       <key>CFBundleTypeRole</key> 
       <string>Viewer</string> 
       <key>LSIsAppleDefaultForType</key> 
       <true/> 
      </dict> 
     </array> 

जोड़ें ... AppDelegate.h

- (BOOL)processFile:(NSString *)file; 
- (IBAction)openFileManually:(id)sender; 

... AppDelegate.m में जोड़े

- (IBAction)openFileManually:(id)sender; 
{ 
    NSOpenPanel *openPanel = [NSOpenPanel openPanel]; 
    NSArray *fileTypes = [NSArray arrayWithObjects:@"xml",nil]; 
    NSInteger result = [openPanel runModalForDirectory:NSHomeDirectory() file:nil types:fileTypes ]; 
    if(result == NSOKButton){ 
     [self processFile:[openPanel filename]]; 
    } 
} 

- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename 
{ 
    return [self processFile:filename]; 
} 

- (BOOL)processFile:(NSString *)file 
{ 
    NSLog(@"The following file has been dropped or selected: %@",file); 
    // Process file here 
    return YES; // Return YES when file processed succesfull, else return NO. 
} 
+0

मैंने CFBundleDocumentTypes (* .xml फ़ाइल) में उचित एक्सटेंशन जोड़े लेकिन मुझे नहीं पता कि प्रतिनिधियों को कैसे कॉन्फ़िगर किया जाए ... उस समय मुझे "selectFile" मिला और मेरा पूरा कोड मेरे selectFile के परिणाम पर निर्भर करता है। .. मैं selectFile और openFile (ड्रैग और ड्रॉप सुविधा को अनुमति देने के लिए) के साथ कैसे काम कर सकता हूं और कॉन्फ़िगर कर सकता हूं ... आपकी मदद के लिए धन्यवाद! – Nono

+0

मैंने कुछ अतिरिक्त जानकारी जोड़ा, यह बताता है कि कोड के एक टुकड़े के साथ दोनों गिराए गए और चयनित फ़ाइलों को कैसे संसाधित करना है। – Anne

+0

महान काम कर रहा है !!! बहुत बहुत धन्यवाद ऐनी! – Nono

संबंधित मुद्दे