使用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];
                });
            });
        });
    });
}); 

但这也不起作用。显示第一个选项卡需要花费相同的时间。
如何解决?

迈科拉·德尼修克(Mykola Denysyuk)

埃姆.. GCD一个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. 第一视图控制器的webView开始异步加载请求;
  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到达后加载其WebView。

有关更多详细信息,请检查NSNotificationCenterUIWebViewDelegate

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章