English 中文(简体)
Getting URL From beginSheetModalForWindow:
原标题:

I m using an OpenPanel to get a file path URL. This works:

[oPanel beginSheetModalForWindow:theWindow completionHandler:^(NSInteger returnCode)
{
 NSURL *pathToFile = nil;

 if (returnCode == NSOKButton)
     pathToFile = [[oPanel URLs] objectAtIndex:0];
}];

This doesn t, resulting in an assignment of read-only variable error:

NSURL *pathToFile = nil;
[oPanel beginSheetModalForWindow:theWindow completionHandler:^(NSInteger returnCode)
{
 if (returnCode == NSOKButton)
     pathToFile = [[oPanel URLs] objectAtIndex:0];
}];
return pathToFile;

In general, any attempt to extract pathToFile from the context of oPanel has failed. This isn t such a big deal for small situations, but as my code grows, I m forced to stuff everything -- XML parsing, core data, etc -- inside an inappropriate region. What can I do to extract pathToFile?

Thanks.

最佳回答

This doesn t, resulting in an assignment of read-only variable error:

NSURL *pathToFile = nil;
[oPanel beginSheetModalForWindow:theWindow completionHandler:^(NSInteger returnCode)
{
 if (returnCode == NSOKButton)
     pathToFile = [[oPanel URLs] objectAtIndex:0];
}];
return pathToFile;

Yes, because you re trying to assign to the copy of the pathToFile variable that gets made when the block is created. You re not assigning to the original pathToFile variable that you declared outside the block.

You could use the __block keyword to let the block assign to this variable, but I don t think this will help because beginSheetModalForWindow:completionHandler: doesn t block. (The documentation doesn t mention this, but there s no reason for the method to block, and you can verify with logging that it doesn t.) The message returns immediately, while the panel is still running.

So, you re trying to have your completion-handler block assign to a local variable, but your method in which you declared the local variable will probably have returned by the time block runs, so it won t be able to work with the value that the block left will leave in the variable.

Whatever you do with pathToFile should be either in the block itself, or in a method (taking an NSURL * argument) that the block can call.

问题回答

you can also runModal after you begin the sheet you just need to make sure you end the sheet later. This way you don t have to bend to apple s will, it isn t deprecated and it should still work perfectly.

NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel beginSheetModalForWindow:window completionHandler:nil];
NSInteger result = [openPanel runModal];
NSURL *url = nil;
if (result == NSFileHandlingPanelOKButton) 
{
    url = [openPanel URL];
}
[NSApp endSheet:openPanel];

It seems a little bit like black magic coding but it does work.





相关问题
Asynchronous data loading in Entity-Framework?

Did anyone hear about asynchronous executing of an EF query? I want my items control to be filled right when the form loads and the user should be able to view the list while the rest of the items ...

Does PHP support asynchronous programming?

I m new to PHP. I am familiar with ASP.NET which support asynchronous programming. That is, if one request needs to do some I/O job. It is suggested to program the web page with BeginProcess/...

How to cancel an asynchronous call?

How to cancel an asynchronous call? The .NET APM doesn t seem to support this operation. I have the following loop in my code which spawns multiple threads on the ThreadPool. When I click a button on ...

What can cause select to block in Python?

Here s a snippet of code I m using in a loop: while True: print loop rlist, wlist, xlist = select.select(readers, [], [], TIMEOUT) print selected # do stuff At a certain point, ...

热门标签