Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone MBProgress HUD在tableView上重新加载数据时崩溃应用程序_Iphone_Objective C_Ios_Uitableview_Mbprogresshud - Fatal编程技术网

Iphone MBProgress HUD在tableView上重新加载数据时崩溃应用程序

Iphone MBProgress HUD在tableView上重新加载数据时崩溃应用程序,iphone,objective-c,ios,uitableview,mbprogresshud,Iphone,Objective C,Ios,Uitableview,Mbprogresshud,当我从web服务下载数据时,我正在使用这段代码在我的一个视图顶部显示MBProgressHUD,唯一的问题是,偶尔这段代码会导致应用程序挂起,在HUD显示“正在下载”且屏幕锁定时什么也不做。此外,如果当我按下刷新按钮(刷新按钮执行下载)时,向用户显示键盘之类的内容,则应用程序会在线崩溃: [self.tableView reloadData]; 我的代码: //Checks for network connection then displays HUD while executing pu

当我从web服务下载数据时,我正在使用这段代码在我的一个视图顶部显示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);
}
//检查网络连接,然后在执行pullAndDisplayData方法时显示HUD
-(i)更新{
UIAlertView*错误视图;
if([[Reachability reachabilityForInternetConnection]currentReachabilityStatus]==NotReachable){
errorView=[[UIAlertView alloc]
initWithTitle:@“网络错误”
消息:@“没有可用的网络连接!”
代表:赛尔夫
取消按钮提示:@“确定”其他按钮提示:无];
[错误视图显示];
}
其他的
{
HUD=[[MBProgressHUD alloc]initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
HUD.delegate=self;
HUD.labelText=@“下载”;
HUD.minSize=CGSizeMake(135.f,135.f);
[HUD showWhileExecuting:@selector(pullAndDisplayData)onTarget:self with Object:nil动画:YES];
}
}
//从web服务下载此用户数据
-(void)pullAndDisplayData{
//不确定模式
ExpenseSdatDownloader*downloader=[[ExpenseSdatDownloader alloc]init];
[下载程序pullAndDisplayData];
NSUserDefaults*默认值=[NSUserDefaults standardUserDefaults];
if([[defaults objectForKey:@“canExportCSVServer”]IsequalString:@“1”])
{
}
[self.tableView重载数据];
//切换到确定模式
HUD.mode=MBPROGRESS HUD mode DEMINATE;
HUD.labelText=@“更新”;
浮动进度=0.0f;
同时(进度<1.0f)
{
进度+=0.01f;
HUD.progress=进度;
美国LEEP(15000);
}
//样本图像基于www.pixelpressicons.com的工作,http://creativecommons.org/licenses/by/2.5/ca/
//将CustomView设置为37×37像素以获得最佳结果(这些是内置进度指示器的界限)
HUD.customView=[[UIImageView alloc]initWithImage:[UIImageName:@“37x Checkmark.png”];
HUD.mode=MBProgressHUDModeCustomView;
HUD.labelText=@“已完成”;
睡眠(2);
}
任何帮助都将不胜感激


Jack

pullAndDisplayData
方法正在单独的线程上运行。这是为了
MBProgressHUD
可以使用UI线程来显示自己。您应该始终从主(UI)线程更新UI。使用
performselectornmainthread:
方法调用
[self.tableView reloadData]和其他UI内容。我假设
[downloader pullAndDisplayData]
是同步调用。

pullAndDisplayData
方法在单独的线程上运行。这是为了
MBProgressHUD
可以使用UI线程来显示自己。您应该始终从主(UI)线程更新UI。使用
performselectornmainthread:
方法调用
[self.tableView reloadData]和其他UI内容。我假设
[downloader pullAndDisplayData]
是同步调用。

问题可能是由于某些内存问题而发生的,您尝试使用
SVProgressHUD
为此,它是
MBProgressHUD的扩展版本:

你只需要这样做:

- (void) pullAndDisplayData{

    [SVProgressHUD showWithStatus:@"Downloading..."];

    // 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);

    [SVProgressHUD dismiss];
}
-(无效)pullAndDisplayData{
[SVProgressHUD showWithStatus:@“下载…”;
//不确定模式
ExpenseSdatDownloader*downloader=[[ExpenseSdatDownloader alloc]init];
[下载程序pullAndDisplayData];
NSUserDefaults*默认值=[NSUserDefaults standardUserDefaults];
if([[defaults objectForKey:@“canExportCSVServer”]IsequalString:@“1”])
{
}
[self.tableView重载数据];
//切换到确定模式
HUD.mode=MBPROGRESS HUD mode DEMINATE;
HUD.labelText=@“更新”;
浮动进度=0.0f;
同时(进度<1.0f)
{
进度+=0.01f;
HUD.progress=进度;
美国LEEP(15000);
}
//样本图像基于www.pixelpressicons.com的工作,http://creativecommons.org/licenses/by/2.5/ca/
//将CustomView设置为37×37像素以获得最佳结果(这些是内置进度指示器的界限)
HUD.customView=[[UIImageView alloc]initWithImage:[UIImageName:@“37x Checkmark.png”];
HUD.mode=MBProgressHUDModeCustomView;
HUD.labelText=@“已完成”;
睡眠(2);
[SVD解散];
}
无需分配或发布任何内容。它就是这样工作的


祝你一切顺利

该问题可能是由于某些内存问题而发生的,您可以尝试使用
SVProgressHUD
为此,它是
MBProgressHUD
的扩展版本:

你只需要这样做:

- (void) pullAndDisplayData{

    [SVProgressHUD showWithStatus:@"Downloading..."];

    // 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);

    [SVProgressHUD dismiss];
}
-(无效)pullAndDisplayData{
[SVProgressHUD showWithStatus:@“下载…”;
//不确定模式
ExpenseSdatDownloader*downloader=[[ExpenseSdatDownloader alloc]init];
[下载程序pullAndDisplayData];
NSUserDefaults*默认值=[NSUserDefaults standardUserDefaults];
if([[defaults objectForKey:@“canExportCSVServer”]IsequalString:@“1”])
{
}
[self.tableView重载数据];
//切换到确定模式
HUD.mode=MBPROGRESS HUD mode DEMINATE;
HUD.labelText=@“更新”;
浮动进度=0.0f;
同时(进度<1.0f)
{
进度+=0.01f;
HUD.progress=进度;
美国LEEP(15000);
}
//样本图像基于www.pixelpressicons.com的工作,http://creativecommons.org/licenses/by/2.5/ca/
//
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
    // download operation here...
    dispatch_async(dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        // reload data here...
    });
});