2011-03-16 13 views
7

anda bir dosyayı açmak için izin benim app inteface bir düğme, burada benim açık koddur vardır: Benim app.h olarakKakao/Obj-C - dosya açma uygulaması simgesine sürükleyerek zaman

:

benim app.m olarak
- (IBAction)selectFile:(id)sender; 

:

@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]; 

nasıl uygulamaya simge sürükle & damla açılmasına izin için kodumu düzenleyebilir?
Not: .plist dosyasını düzenledim ve "xml" için bir satır ekledim ancak bir şey değiştirdim, dosyam simgeye bırakıldığında bir hata oluştu. Benim app Yardımlarınız için değil belge tabanlı uygulama


Teşekkür: wich kodumu
Not 3 bakınız: -:
Not 2 "> Aç ... Dosya" selectFile I bağlantılı !
Miskia

+0

Neden NSDocument'i kullanmıyorsunuz? Yeterince esnek değil misiniz? –

cevap

15

İlk .plist dosya içindeki CFBundleDocumentTypes uygun uzantıları ekleyin. OpenFile: (bir dosya düştü)
- Uygulama: openfiles: (birden fazla dosya düştü)

Referans:
NSApplicationDelegate Protocol Reference

- Uygulama
:

Sonraki aşağıdaki delegelere uygulamak Yoruma yanıt:

Adım adım sınav için ... AppDelegate.h

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

... AppDelegate.m ekle ekle

<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> 

: ple, umarım açıkça her şeyi :)

.plist dosyaya ekleme yapar

- (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 dosyası) için uygun uzantıları ekledim Ama ben temsilciler nasıl yapılandırılacağını anlamıyorum ... O zaman bir "selectFile" aldım ve tüm kodumu selectFile sonucuna bağlıdır. .. selectFile AND openFile (sürükle ve bırak özelliğine izin vermek için) ile nasıl çalışabilirim ve yapılandırabilirim ... Yardımlarınız için teşekkürler! – Nono

+0

Bazı ek bilgiler ekledim, bu hem bırakılmış hem de seçili dosyaları tek bir kod parçasıyla nasıl işleyeceğinizi açıklıyor. – Anne

+0

Harika çalışıyor! Çok teşekkürler Anne! – Nono

İlgili konular