2015-06-19 54 views
1

有沒有辦法強制NSOpenPanel關閉,以便在調試時看到屏幕?在調試過程中,我無法在Xcode中看到代碼,我也無法移動面板。NSOpenPanel在調試期間保持打開狀態

這是我有:

- (IBAction)openImage:(id)sender { 
    NSArray* fileTypes = [[NSArray alloc] initWithObjects:@"jpg", @"JPG", nil]; 

    NSOpenPanel *panel = [NSOpenPanel openPanel]; 
    [panel setCanChooseDirectories:NO]; 
    [panel setCanChooseFiles:YES]; 
    [panel setAllowsMultipleSelection:NO]; 
    [panel setAllowedFileTypes:fileTypes]; 

    [panel beginWithCompletionHandler:^(NSInteger result) { 
     if (result == NSFileHandlingPanelOKButton) { 

      self.image = [[NSImage alloc] initWithContentsOfURL:panel.URL]; 
      [panel close]; 


      [self doSomethingWithImage]; 

     }else{ 
     } 
    }]; 
} 

- (void) doSomethingWithImage { 
    // if I put a breakpoint here, 
    // the NSOpenPanel is still on the screen and I can't move it. 

} 

回答

0

一個簡單的解決方法是在主隊列調度-doSomethingWithImage所以執行-doSomethingWithImage之前完成處理結束(和對話框關閉)。

[panel beginWithCompletionHandler:^(NSInteger result) { 
    if (result == NSFileHandlingPanelOKButton) { 

     self.image = [[NSImage alloc] initWithContentsOfURL:panel.URL]; 
     [panel close]; 

     dispatch_async(dispatch_get_main_queue(), {() -> Void in 

     [self doSomethingWithImage]; 

     }); 

    }else{ 
    } 
}]; 
+0

我試過這個,它不適合我。 – Alex311

1

個人而言,我發現了兩個方法:

  1. 如果你開始你的應用程序,無論是移動應用程序或Xcode到另一個桌面。所以,當你運行你的應用程序時,它會跳到另一個桌面(Xcode所在位置),當你到達一箇中斷點時,打開的面板不會妨礙Xcode

  2. 使用beginSheetModalForWindow:編碼幾乎相同,但面板在調試時不會隱藏部分Xcode。作爲與窗口相關的選擇面板運行OpenPanel。對於beginSheetModalForWindow,您必須指明相關窗口(通常是主窗口),這是唯一需要的附加編碼。

+0

我改變了我的面板顯示爲一張表,它解決了我的這個問題。謝謝! – Alex311

相關問題