【发布时间】:2012-05-16 02:40:16
【问题描述】:
我试图阻塞主线程直到 UIWebView 完成加载,但在调用 [UIWebView loadRequest:] 后调用 [NSThread sleepForTimeInterval:] 会使加载永远不会完成:在睡眠完成后调用 webViewDidFinishLoad 委托。
为什么会这样?以及如何阻止主线程?
我从[ViewController willRotateToInterfaceOrientation:] 调用loadPage,我想做的是防止iOS 界面旋转,直到加载了另一个方向的webview。
- (void)loadPage {
UIWebView* webview2 = [[UIWebView alloc] initWithFrame:rect];
webview2.delegate = self;
NSString* htmlPath = ..... ; // Path to local html file
NSURL *newURL = [[[NSURL alloc] initFileURLWithPath: htmlPath] autorelease];
NSURLRequest *newURLRequest = [[[NSURLRequest alloc] initWithURL: newURL] autorelease];
[webview2 loadRequest:newURLRequest];
[NSThread sleepForTimeInterval:10.0f]; // Why this blocks the uiwebview thread ??
[self.view addSubview:webview2];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(@"Finished loading!");
}
【问题讨论】:
-
不是你要找的,但为什么不在
-(void)webViewDidFinishLoad:(UIWebView *)webView中调用[[self view] addSubview:webView];? -
我希望界面在 uiwebview 加载完成后旋转。