活动指示器未显示

时间:2014-04-17 20:30:46

标签: ios objective-c uialertview uiactivityindicatorview

我想在下载过程中显示活动指示符。但是,当我单击开始下载的按钮时,我会看到单击的按钮,直到下载完成。我做错了什么?

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
  if (buttonIndex == 1) {

    //Update begins
    UIActivityIndicatorView *activityIndicatorView = [[UIActivityIndicatorView alloc] init];
    UIView *overlayView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];

    overlayView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2];
    activityIndicatorView.center = overlayView.center;
    [overlayView addSubview:activityIndicatorView];
    [self.tabBarController.view addSubview:overlayView];
    [activityIndicatorView startAnimating];

    UpdateModel *updateModel = [[UpdateModel alloc] init];
    [updateModel deleteOldDatabase];
    [updateModel downloadNewDatabase];

    //Update done
    [overlayView removeFromSuperview];
    [activityIndicatorView removeFromSuperview];
    [activityIndicatorView stopAnimating];
  }
}

1 个答案:

答案 0 :(得分:0)

我猜那些是封锁过程?

[updateModel deleteOldDatabase];
[updateModel downloadNewDatabase];

我想问题只是您使用数据库更新阻止了应用程序的UI。尝试在这样的背景上进行工作:

//Update begins
    UIActivityIndicatorView *activityIndicatorView = [[UIActivityIndicatorView alloc] init];
    UIView *overlayView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];

    overlayView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2];
    activityIndicatorView.center = overlayView.center;
    [overlayView addSubview:activityIndicatorView];
    [self.tabBarController.view addSubview:overlayView];
    [activityIndicatorView startAnimating];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        UpdateModel *updateModel = [[UpdateModel alloc] init];
        [updateModel deleteOldDatabase];
        [updateModel downloadNewDatabase];

        dispatch_async(dispatch_get_main_queue(), ^{
            //Update done
            [overlayView removeFromSuperview];
            [activityIndicatorView removeFromSuperview];
            [activityIndicatorView stopAnimating];
        });
    });

永远不要忘记必须在主线程上完成所有UI处理。这就是为什么在完成数据加载和处理之后,我们再次调用dispatch_async但是使用“dispatch_get_main_queue()”