2010-02-19 51 views
4

我在窗口控制器以下代碼作爲一個單專用web瀏覽器的一部分web視圖的mainFrame方法返回零

+ (id)sharedPurchaseController { 
    static SomeController *sharedController = nil; 

    if (!sharedController) { 
     sharedController = [[SomeController alloc] initWithWindowNibName:@"anXIB"]; 
    } 

    return sharedController; 
} 

- (void)awakeFromNib{ 
    shouldReloadWeb = NO; 
    [progressIndicator startAnimation:nil]; 
    NSString* urlText = [NSString stringWithString:@"http://www.google.com"]; 
    [[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlText]]]; 
} 

-(void)windowWillClose:(NSNotification *)notification{ 
    shouldReloadWeb = YES; 
} 

- (void)windowDidBecomeKey:(NSNotification *)notification{ 
     if (shouldReloadWeb){ 
     WebFrame* aFrame = [webView mainFrame]; //<-- Returns nil second time 
     [[webView mainFrame] reload]; 
     shouldReloadWeb = NO; 
    } 
} 

的問題是,當NSWindow(瀏覽器)被關閉,然後重新打開,第二次,[webView mainFrame]返回零。 NSWindow出現在屏幕上,但WebView不響應。

如果我註釋掉創建單例的代碼,一切都按預期工作。

有沒有用Web應用程序創建一個單獨的瀏覽器,同時保持WebView在Nib中?

感謝,-Ben

回答

3

默認情況下,當關閉其容器窗口WebView關閉。當WebView關閉時,它將卸載當前頁面,停止加載任何當前請求,不再處理新請求或發送委託消息。如果你想要當窗口重新打開的WebView留下來,因此它可以被重用,你應該叫

[webView setShouldCloseWithWindow:NO]; 

-awakeFromNib方法。

+0

+1今天剛剛有這個問題。有一些超級遲到的提議。 – Dominic