我一直在尋找解決方案,現在我可以使用以下代碼在模態會話上使用WebView。如果不使用-runMode:beforeDate
我的WebView無法處理的鍵盤或安裝事件:
- (void) OpenURL:(const char *)_url
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[NSApplication sharedApplication];
[NSApp setDelegate:self];
NSString *url = [NSString stringWithUTF8String:_url];
NSLog(@"OpenURL: %@", url);
NSRect windowRect = NSMakeRect(10.0f, 10.0f, 800.0f, 600.0f);
NSWindow *window = [[NSWindow alloc] initWithContentRect:windowRect
styleMask:(NSResizableWindowMask|NSClosableWindowMask|NSTitledWindowMask)
backing:NSBackingStoreBuffered defer:NO];
[window setDelegate:self];
WebView *webview = [[WebView alloc] initWithFrame:windowRect
frameName:@"mainFrame"
groupName:nil];
[webview setFrameLoadDelegate:self];
[[webview mainFrame]
loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
[window setContentView:webview];
[window makeKeyAndOrderFront:nil];
// Modal Session
NSModalSession session = [NSApp beginModalSessionForWindow:window];
_result = NSModalResponseContinue;
while (_result == NSModalResponseContinue) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
_result = [NSApp runModalSession:session];
// The event loop is a runloop data source, so any ui event will
// wake up the source and make this method returns, and so
// you can block the run loop and tell him to wait that
// something append. [2]
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
beforeDate:[NSDate distantFuture]];
[self doSomeWork];
[pool drain];
}
[NSApp endModalSession:session];
[pool release];
}
你需要調用[NSApp stopModal]
,[NSApp abortModal]
或[NSApp stopModalWithCode:yourReturnCode]
的地方是這樣的:
- (void)windowWillClose:(NSNotification *)notification
{
NSLog(@"windowWillClose");
[NSApp stopModal];
}
鏈接:
此網站是否有2小時前?說真的,我不知道我怎麼會錯過這個。非常感謝! – Dodo 2010-11-12 15:13:09
@Dodo:對我來說更容易(重新)發現我猜 - 我已經經歷了這個問題的更多人爲的版本:) – 2010-11-12 15:30:53