开发者

WebView mainFrame method returns nil

开发者 https://www.devze.com 2022-12-20 11:48 出处:网络
I have the following code in a window controller as part of a singleton special web browser + (id)sharedPurchaseController {

I have the following code in a window controller as part of a singleton special web browser

+ (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;
    }
}

The problem is that when the NSWindow (browser) is closed and then re-open, for a second time, [webView mainFrame] returns nil. The NSWindow appears on screen, but the WebView is non-responsive.

If I comment out the code that creates a singleton, everything works as expected.

Is there away to create a singleton browser with an application while keeping the WebView in the Nib?

Thanks, -Ben


By default, a WebView is closed when its container window is closed. When a WebView is closed, it unloads the current page, stops loading any current requests and no longer processes new requests nor sends delegate messages. If you want the WebView to stick around so it can be reused when the window is reopened, you should call

[webView setShouldCloseWithWindow:NO];

in your -awakeFromNib method.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号