I'm loading a remote URL in a Webview, and I want to show a spinner while the content is loading. I have the following code, but the spinner doesn't go away when the content finishes loading. How do I make it so the spinner disappears after the content is loaded?
NSURL *noteURL = [NSURL URLWithString:@"http://google.com/"];
NSString *defaultNote = @"Hello there";
NSRect frame = [[noteView superview] frame];
noteSpinner = [[[NSProgressIndicator alloc] initWithFrame:NSMakeRect(NSMidX(frame)-16, NSMidY(frame)-16, 32, 32)] autorelease];
[noteSpinner setStyle:NSProgressIndicatorSpinningStyle];
[noteSpinner startAnimation:self];
//webViewFinishedLoading = NO;
[[noteView superview] addSubview:noteSpinner];
if (noteURL)
{
if ([noteURL isFileURL])
{
[[noteView mainFrame] loadHTMLString:@"Release notes with file:// URLs are not supported for security reasons—Javascript would be able to read files on your file system." baseURL:nil];
}
else
{
[[noteView mainFrame] loadRequest:[NSURLRequest requestWithURL:noteURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30]];
}
}
e开发者_开发技巧lse
{
[[noteView mainFrame] loadHTMLString:defaultNote baseURL:nil];
}
Be the frame load delegate, and stop the progress indicator's animation when the main frame finishes loading.
Only stop the animation when the frame that you received a did-finish-load message for is the web view's main frame. You'll get one of those messages for every frame in the web view, including nested frames and iframes. You don't want to stop the animation prematurely.
精彩评论