有没有一种方法可以强制关闭NSOpenPanel,以便在调试时可以看到屏幕?在调试时,我看不到Xcode背后的代码,也不能移动面板。

这就是我所拥有的:

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

}

最佳答案

一个简单的解决方法是在主队列上安排-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{
    }
}];

关于xcode - NSOpenPanel在调试期间保持打开状态,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30948506/

10-10 20:14