当我从网络服务下载数据时,我正在使用这段代码在我的一个 View 的顶部显示 MBProgressHUD,唯一的问题是偶尔这段代码会导致应用程序挂起,同时什么都不做HUD 显示“正在下载”并且屏幕被锁定。另外,如果我在按下刷新按钮(刷新按钮执行下载)时向用户显示类似键盘的东西,那么应用程序就会崩溃:
[self.tableView reloadData];
我的代码:
//Checks for network connection then displays HUD while executing pullAndDisplayData method
- (IBAction) update {
UIAlertView *errorView;
if([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] == NotReachable) {
errorView = [[UIAlertView alloc]
initWithTitle: @"Network Error"
message: @"No Network connection availible!"
delegate: self
cancelButtonTitle: @"OK" otherButtonTitles: nil];
[errorView show];
}
else
{
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
HUD.delegate = self;
HUD.labelText = @"Downloading";
HUD.minSize = CGSizeMake(135.f, 135.f);
[HUD showWhileExecuting:@selector(pullAndDisplayData) onTarget:self withObject:nil animated:YES];
}
}
//Downloads this users data from the web-service
- (void) pullAndDisplayData{
// Indeterminate mode
ExpensesDataDownloader *downloader = [[ExpensesDataDownloader alloc] init];
[downloader pullAndDisplayData];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([[defaults objectForKey:@"canExportCSVServer"] isEqualToString:@"1"])
{
}
[self.tableView reloadData];
// Switch to determinate mode
HUD.mode = MBProgressHUDModeDeterminate;
HUD.labelText = @"Updating";
float progress = 0.0f;
while (progress < 1.0f)
{
progress += 0.01f;
HUD.progress = progress;
usleep(15000);
}
// The sample image is based on the work by www.pixelpressicons.com, http://creativecommons.org/licenses/by/2.5/ca/
// Make the customViews 37 by 37 pixels for best results (those are the bounds of the build-in progress indicators)
HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
HUD.mode = MBProgressHUDModeCustomView;
HUD.labelText = @"Completed";
sleep(2);
}
如有任何帮助,我们将不胜感激。
jack
最佳答案
pullAndDisplayData
方法在单独的线程上运行。这样 MBProgressHUD
就可以使用 UI 线程来显示自己。您应该始终从主 (UI) 线程更新您的 UI。使用 performSelectorOnMainThread:
方法调用 [self.tableView reloadData];
和其他 UI 内容。我假设 [downloader pullAndDisplayData];
是同步调用。
关于iphone - MBProgress HUD 在 tableView 上的 reloadData 上崩溃应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11411680/