我试过打电话
modalSession=[NSApp beginModalSessionForWindow:conversionWindow];
[NSApp runModalForWindow:conversionWindow];
为了获得一个模态转换窗口,阻止用户与应用程序的其余部分交互,但这似乎也会阻止代码执行。我的意思是上面显示的代码后面的代码根本没有执行。我怎样才能解决这个问题?我确信这是可能的,因为许多应用程序在执行一些大任务(例如视频转换等)时显示出一些进展......
除非绝对必要,否则请不要使用应用程序模式窗口。如果可能的话,使用一张纸。但是,如果必须使用模式对话框,则可以在模式对话框打开时给主运行循环一些时间来运行它:
NSModalSession session = [NSApp beginModalSessionForWindow:[self window]];
int result = NSRunContinuesResponse;
while (result == NSRunContinuesResponse)
{
//run the modal session
//once the modal window finishes, it will return a different result and break out of the loop
result = [NSApp runModalSession:session];
//this gives the main run loop some time so your other code processes
[[NSRunLoop currentRunLoop] limitDateForMode:NSDefaultRunLoopMode];
//do some other non-intensive task if necessary
}
[NSApp endModalSession:session];
如果您有需要主运行循环来操作的视图,这非常有用(WebView
我想到了)。
但是,请理解模态会话就是这样,任何代码after打电话给beginModalSessionForWindow:
在模态窗口关闭并且模态会话结束之前不会执行。这是不使用模式对话框的一个很好的理由。
请注意,您不得在while
在上面的代码中循环,因为这样您将阻止模态会话以及主运行循环,这会将您的应用程序变成沙滩球城市。
如果你想在后台做一些实质性的事情,你必须使用某种形式的并发,例如 usingNSOperation
、GCD 后台队列或只是一个普通的后台线程。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)