tags:

views:

31

answers:

1

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

+2  A: 

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.

Rob Keniger