- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我试图阻塞主线程直到 UIWebView 完成加载,但是在调用 [UIWebView loadRequest:]
之后调用 [NSThread sleepForTimeInterval:]
使加载永远不会complete:webViewDidFinishLoad
委托(delegate)在 sleep 结束后被调用。
为什么会这样?以及如何阻塞主线程?
我正在从 [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!");
}
最佳答案
我认为您正在尝试解决错误的问题。
你不想阻塞主线程。首先,整个 UI 是在主线程上绘制的,因此看起来您的应用程序已挂起。其次,UIWebView 是 UI 的一部分(这可能就是您所做的不起作用的原因)。
那么,怎么办?
看看下面的方法:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
当您开始加载您的页面时,您可以从此方法返回 NO
。加载完成后,您可以正常返回值。 UIWebView
有一个委托(delegate)告诉您状态。
关于iphone - 阻塞主线程直到 UIWebView 完成加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10484629/
有人可以向我澄清主线 DHT 规范中的声明吗? Upon inserting the first node into its routing table and when starting up th
我正在尝试使用 USB 小工具驱动程序使嵌入式设备作为 MTP 设备工作。 我知道 Android 从大容量存储设备切换到 MTP 设备已经有一段时间了,并且找到了 source code for M
我是一名优秀的程序员,十分优秀!