我有以下代码:
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];
Run Code Online (Sandbox Code Playgroud)
for循环需要一段时间才能执行,因为它会在应用程序第一次运行时下载一些信息.因此,我希望此通知显示,以便用户不会坐在无响应的屏幕上等待.问题是在for循环结束之前,alertview不会显示.然后它马上就消失了.我需要改变什么?
Nit*_*hel 11
alert-view在.h类中声明您的Object,以便在.m类中使用.
将您的for循环代码放入performSelectorInBackgroundBackgroud 中 的运行循环中,这样您就可以不等待ForLoop的完成了.
[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];
}
Run Code Online (Sandbox Code Playgroud)
其他方案
您也可以根据您的代码使用Grand Central Dispatch(GCD),如下所示: -
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];
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1521 次 |
| 最近记录: |