我有四个选项卡 Controller ,其中一个在 View Controller 中有一个 UIWebView。当应用程序启动时,我创建了一个新线程来在我的 webView 中加载 Web 内容,因此当 Web 完成加载时,当用户切换到 View Controller 时,它已准备好供用户查看。
我的问题是,webVIew 根本没有在新创建的线程中加载请求,但是当我将 loadRequest 放入 viewDidLoad 时它正在工作。我花了整整一天的时间来寻找解决方案,但一点运气也没有。
这是我的代码:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
[NSThread detachNewThreadSelector:@selector(doStuff) toTarget:self withObject:nil];
return self;
}
- (void)doStuff
{
NSLog(@"Starting a new thread ...");
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
url = [NSURL URLWithString:@"http://www.myURL.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[newsWebView loadRequest:request];
[pool release];
}
有人可以解决我的问题吗?非常感谢。
最佳答案
- (void)doStuff
{
NSLog(@"Starting a new thread ...");
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
url = [NSURL URLWithString:@"http://www.myURL.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[newsWebView loadRequest:request];
});
[pool release];
}
关于ios - 在新线程中加载 UIWebView 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11857858/