0

我有四个选项卡控制器,其中一个在视图控制器中有一个 UIWebView。当应用程序启动时,我创建了一个新线程来在我的 webView 中加载 web 内容,因此当 web 完成加载时,它就可以供用户在切换到视图控制器时查看。

我的问题是,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];
 }

有人可以解决我的问题吗?非常感谢。

4

3 回答 3

2
 - (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];
}
于 2015-08-18T07:20:20.420 回答
1

UIWebView 是 UIKit 的一部分,所以你应该在主线程上操作。

- (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_main_queue(), ^(void){
         [newsWebView loadRequest:request];
     });
     [pool release];
 }
于 2012-08-08T05:09:52.717 回答
-1

尝试这个

func loadPdfFromDocumentDirectory(localPathUrl: URL)
{
    let urlRequest = URLRequest(url: localPathUrl)
    DispatchQueue.main.async {
        self.webView?.loadRequest(urlRequest)
    }
}
于 2018-01-17T09:14:40.840 回答