The MainFrame WebView method returns nil

I have the following code in a window controller as part of a single 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) closes and then opens again, [webView mainFrame] returns nil. NSWindow appears on the screen, but the WebView does not respond.

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

Is it possible to create a singleton browser with an application while storing a WebView in Nib?

Thanks, -Ben

+4
source share
1 answer

By default, a WebView closes when the container window closes. When a WebView closes, it unloads the current page, stops loading current requests and no longer processes new requests and does not send delegate messages. If you want the WebView to stick so that it can be reused when you reopen the window, you should call

 [webView setShouldCloseWithWindow:NO]; 

in your method -awakeFromNib .

+3
source

Source: https://habr.com/ru/post/1301777/


All Articles