I don't understand why the panel won't close immediately when i click OK button.

if (NSOKButton == [uploadPanel runModal]) {
    for (int i = 0; i < 10000; i++) {
        NSLog(@"%d",i);
    }
}

Instead, it closes only until all statements in the scope are exacuted.

It is weird that the result turn out the SAME when I change the code into something like this:

if (NSOKButton == [uploadPanel runModal]) {
    NSLog(@"ok");
}

for (int i = 0; i < 10000; i++) {
    NSLog(@"%d",i);
}

By the way, there is NO such problem with NSSavePanel.

Is there any way to solve the problem?

UPDATE1: I have tried the method provided by Abhi Beckert, but still doesn't work :(

if (NSOKButton == [uploadPanel runModal]) {
    NSLog(@"ok");
    [uploadPanel close];
}

for (int i = 0; i < 10000; i++) {
    NSLog(@"%d",i);
}
有帮助吗?

解决方案 2

Get the solution myself. Although the ideal solution is to put the loop in another thread, it is too difficult to handle the thread safety things. An alternative solution is to execute the code in a delayed way. (like shown below)

-(IBAction)upload:(id)sender {
    NSOpenPanel *uploadPanel = [NSOpenPanel openPanel];
    if ([uploadPanel runModal] == NSOKButton) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC / 100), dispatch_get_main_queue(), ^(void){
            for (int i = 0; i < 10000; i++) {
                NSLog(@"%d",i);
            }
        });
    }
}

其他提示

It doesn't close immediately because it doesn't. There's no reason, that's just how it works.

If ou need it to close earlier then close it manually:

[uploadPanel close];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top