gpt4 book ai didi

ios - 使用 GCD 连续显示多个 View

转载 作者:行者123 更新时间:2023-11-28 21:50:47 24 4
gpt4 key购买 nike

我有 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];
}
}
}

但是主线程等待所有标签页加载。
我使用 dispatch_async

GCD 进行了尝试
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];
});
});
});
});
});

但这也行不通。显示第一个标签需要同样的时间。
如何解决?

最佳答案

嗯.. GCDUIWebView 的 -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. 第一个 View Controller 的 webView 开始加载请求异步;
  2. 当请求加载(或失败)时,webView 会通知您代表;
  3. 使用 webView 通知所有其他 View Controller ,让它们开始加载他们的请求;

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

// 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];
}

之后,“订阅”所有其他(仅选项卡) View Controller 以获取 FirstViewControllerDidLoadWebView 通知,并在通知到达后加载它们的 WebView。

更多详情请查看NSNotificationCenterUIWebViewDelegate

关于ios - 使用 GCD 连续显示多个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28425745/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com