2012-02-02 31 views
0

我正在使用om我的第一臺Mac應用程序,並且它很棒。除了一個問題。 我的應用程序使用PNG圖像來創建新圖像,可以將他/她的PNG圖像拖放到一個框中,並顯示NSOpenPanel選擇保存位置。應用程序:openFile:不會在啓動時調用

但是,當我退出應用程序並右鍵單擊PNG圖像並選擇我的應用程序時,不顯示NSOpenPanel。 如果應用程序是活動的,我也是這樣做的..沒問題..它顯示了NSOpenPanel,它可以做任何它需要處理的圖像。只是不在推出。

我的問題:爲什麼不呢?

我的代碼:
的AppDelegate:

- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename 
{ 
    NSURL *fileURL = [NSURL URLWithString:[NSString stringWithFormat:@"file://localhost/%@", filename]]; 

    [_infoLabel setTitleWithMnemonic:NSLocalizedString(@"Drag SOMETHING SOMETING here!", @"AppDelegate")]; // Debug text 

    BOOL returnValue = [Resizer loadFilename:fileURL]; 

    if (returnValue) 
    { 
     [_dragDrop setImage:[[NSImage alloc] initWithContentsOfFile:filename]]; 
    } 

    return returnValue; 
} 

- (void)didLoadSourceImageSuccessfull:(NSImage *)image 
{ 
    [Resizer selectDestinationPath]; 
} 

調整器類:

+ (BOOL)loadFilename:(NSURL *)pathToFile 
{ 
    _sourcePath = pathToFile; 

    if ([self sourceFileIsValid]) 
    { 
     [self didLoadSourceImageSuccessfull:[[NSImage alloc] initWithContentsOfURL:_sourcePath]]; 
     return TRUE; 
    } 
    else { 
     _sourcePath = nil; 
     return FALSE; 
    } 
} 

+ (void)selectDestinationPath 
{ 
    NSOpenPanel* openDlg = [NSOpenPanel openPanel]; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(destinationSavePanelDidClose:) 
               name:NSWindowDidEndSheetNotification 
               object:_window]; 

    [openDlg setCanChooseFiles:NO]; 
    [openDlg setCanCreateDirectories:YES]; 
    [openDlg setCanChooseDirectories:YES]; 
    [openDlg setCanSelectHiddenExtension:NO]; 

    [openDlg beginSheetModalForWindow:_window completionHandler:^(NSInteger result) { 
     if (result == NSFileHandlingPanelOKButton) { 
      _destinationpath = [[openDlg URLs] objectAtIndex:0]; 
     } 
    }]; 
} 
+0

我們可以看到'AppDelegate.h'和'Resizer.h'嗎?另外,在'+ [Resizer loadFilename:]'你調用'[self didLoadSourceImageSuccessfull:...]',這將等同於'+ [Resizer didLoadSourceImageSuccessfull:]':這個方法的實現在哪裏(我猜我是混淆了爲什麼'AppDelegate'有一個'-didLoadSourceImageSuccessfull:'實例方法,'Resizer'有'+ didLoadSourceImageSuccessfull:'類方法)?此外,在'Resizer'類中,您可以直接從類方法內訪問'_sourcePath','_destinationpath'和'_window';這些定義在哪裏? – NSGod 2012-02-02 23:56:29

+0

您可能需要考慮一個基於文檔的應用程序,它可以爲您處理大部分的應用程序。 (我不會全力以赴,所以我不知道這是否適合你。) – 2012-02-03 00:02:00

回答

0

也許,你正試圖打開對話框之前你_window顯示或加載。 嘗試在你的窗口控制器或應用程序委託的方法-(void)awakeFromNib打開對話框,例如:

- (void)awakeFromNib { 
    if (isFileSentToApplication) [Resizer selectDestinationPath]; 
} 

如果你的控制器類是NSWindowController一個子類,你也可以做到這一點在-(void)windowDidLoad

相關問題