ios: UIAlertView 在 for 循环执行之前立即显示 [英] ios: UIAlertView show immediately before for loop executes

查看:14
本文介绍了ios: UIAlertView 在 for 循环执行之前立即显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Loading Content For the First Time..."
                                                   message:@"\n"
                                                  delegate:self
                                         cancelButtonTitle:nil
                                         otherButtonTitles:nil];

            UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
            spinner.center = CGPointMake(139.5, 75.5); // .5 so it doesn't blur
            [alertView addSubview:spinner];
            [spinner startAnimating];
            [alertView show];

            for (TCMLevelRemote *level in [obj objectForKey:@"levels"]){
                [[TCMExhibitFeedStore sharedStore] createLevel:level];
            }
            [[TCMExhibitFeedStore sharedStore] loadAllLevels];
            [[TCMExhibitFeedStore sharedStore] setAllLevels:[[TCMExhibitFeedStore sharedStore] storedLevels]];
            [alertView dismissWithClickedButtonIndex:0 animated:YES];

for 循环需要一段时间才能执行,因为它会在应用程序第一次运行时下载一些信息.所以我希望显示此通知,以便用户不会坐在无响应的屏幕前等待.问题是警报视图直到 for 循环结束才显示.然后它就马上消失了.我需要改变什么?

The for loops takes a while to execute because it downloads some information for the first time the app runs. So I want this notification to show so the user doesn't sit waiting at an unresponsive screen. The problem is the alertview doesn't show until the for loop ends. Then it just goes away right away. What do I need to change?

推荐答案

.h 类中声明你的 alert-view 对象,以便在 .m 中的任何地方使用 类.

Declare your alert-view Object in .h class for using everywhere in .m class.

将您的 for 循环代码放在 performSelectorInBackground 中,以便在 Backgroud 中运行循环,这样您的 Alertview 就不会等待您的 ForLoop 完成.

Put your for-loop code in performSelectorInBackground for running loop in Backgroud so you Alertview Not waiting for Your ForLoop finishing.

 [self performSelectorInBackground: @selector(LoadForLoop) withObject: nil];

-(void)LoadForLoop
{
 for (TCMLevelRemote *level in [obj objectForKey:@"levels"]){
                [[TCMExhibitFeedStore sharedStore] createLevel:level];
            }
            [[TCMExhibitFeedStore sharedStore] loadAllLevels];
            [[TCMExhibitFeedStore sharedStore] setAllLevels:[[TCMExhibitFeedStore sharedStore] storedLevels]];
            [alertView dismissWithClickedButtonIndex:0 animated:YES];

}

其他解决方案

您也可以使用 Grand Central Dispatch(GCD) 根据您的代码如下所示:-

You can also use Grand Central Dispatch (GCD) like bellow as per your code:-

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Loading Content For the First Time..."
                                                   message:@"\n"
                                                  delegate:self
                                         cancelButtonTitle:nil
                                         otherButtonTitles:nil];

            UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
            spinner.center = CGPointMake(139.5, 75.5); // .5 so it doesn't blur
            [alertView addSubview:spinner];
            [spinner startAnimating];
            [alertView show];



dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
          for (TCMLevelRemote *level in [obj objectForKey:@"levels"]){
                [[TCMExhibitFeedStore sharedStore] createLevel:level];
            }
            [[TCMExhibitFeedStore sharedStore] loadAllLevels];
            [[TCMExhibitFeedStore sharedStore] setAllLevels:[[TCMExhibitFeedStore sharedStore] storedLevels]];

        dispatch_async(dispatch_get_main_queue(), ^{
            [spinner StopAnimating];
            [alertView dismissWithClickedButtonIndex:0 animated:YES];
        });
    });

这篇关于ios: UIAlertView 在 for 循环执行之前立即显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆