使用GCD连续显示多个视图

我有4个选项卡,都有UIWebView 。 我希望第一个选项卡应该加载并立即显示,而无需等待其他人加载。 为此我在UITabBarController类中做了这个:

 for(UIViewController * viewController in self.viewControllers){ if(![[NSUserDefaults standardUserDefaults]boolForKey:@"isSessionExpired"]) { if((int)[self.viewControllers indexOfObject:viewController] != 4) { viewController.tabBarItem.tag = (int)[[self viewControllers] indexOfObject:viewController]; [viewController view]; } } } 

但主线程等待所有制表符加载。
我试着用GCD使用dispatch_async

 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ // 1 dispatch_async(dispatch_get_main_queue(), ^{ UIViewController *firstContrl = [self.viewControllers objectAtIndex:0]; firstContrl.tabBarItem.tag = (int)[[self viewControllers] indexOfObject:firstContrl]; [firstContrl view]; dispatch_async(dispatch_get_main_queue(), ^{ // 2 UIViewController *secContrl = [self.viewControllers objectAtIndex:1]; secContrl.tabBarItem.tag = (int)[[self viewControllers] indexOfObject:secContrl]; [secContrl view]; dispatch_async(dispatch_get_main_queue(), ^{ // 3 UIViewController *thirdContrl = [self.viewControllers objectAtIndex:2]; thirdContrl.tabBarItem.tag = (int)[[self viewControllers] indexOfObject:thirdContrl]; [thirdContrl view]; dispatch_async(dispatch_get_main_queue(), ^{ // 4 UIViewController *fourthContrl = [self.viewControllers objectAtIndex:3]; fourthContrl.tabBarItem.tag = (int)[[self viewControllers] indexOfObject:fourthContrl]; [fourthContrl view]; }); }); }); }); }); 

但是这也行不通。 显示第一个标签需要相同的时间。
这怎么解决?

-loadRequest:UIWebView的 -loadRequest:不像你想象的那样工作。 上面的所有代码都相当于:

 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ dispatch_async(dispatch_get_main_queue(), ^{ UIViewController *firstContrl = [self.viewControllers objectAtIndex:0]; firstContrl.tabBarItem.tag = (int)[[self viewControllers] indexOfObject:firstContrl]; [firstContrl view]; UIViewController *secContrl = [self.viewControllers objectAtIndex:1]; secContrl.tabBarItem.tag = (int)[[self viewControllers] indexOfObject:secContrl]; [secContrl view]; UIViewController *thirdContrl = [self.viewControllers objectAtIndex:2]; thirdContrl.tabBarItem.tag = (int)[[self viewControllers] indexOfObject:thirdContrl]; [thirdContrl view]; UIViewController *fourthContrl = [self.viewControllers objectAtIndex:3]; fourthContrl.tabBarItem.tag = (int)[[self viewControllers] indexOfObject:fourthContrl]; [fourthContrl view]; }); }); 

为了解决你的问题,你需要实现队列取决于- (void)webViewDidFinishLoad:(UIWebView *)webView 。 这个想法是:

  1. 第一个视图控制器的webViewasynchronous启动加载请求;
  2. 当请求被加载(或失败),webView将通知你的代表;
  3. 用webView通知所有其他视图控制器,让他们开始加载他们的请求;

如果我是你,我会用下面的方式来实现它:

 // FirstViewController.h extern NSString * const FirstViewControllerDidLoadWebView; // FirstViewController.m NSString * const FirstViewControllerDidLoadWebView=@"FirstViewControllerDidLoadWebView"; - (void)webViewDidFinishLoad:(UIWebView *)webView { // notify other controllers, to let them load their web views: [[NSNotificationCenter defaultCenter] postNotificationName:FirstViewControllerDidLoadWebView object:nil]; } - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { // loading failed. Try to load it few more times but anyway notify other controllers after: [[NSNotificationCenter defaultCenter] postNotificationName:FirstViewControllerDidLoadWebView object:nil]; } 

之后,“订阅” FirstViewControllerDidLoadWebView通知的所有其他(仅限制表)视图控制器,并在到达之后加载WebView。

有关更多详细信息,请查看NSNotificationCenter和UIWebViewDelegate