0

我正在同时调用两个 Web 服务,当时我正在显示活动指示器。由于任何一项服务都可以先结束。我不确定如何隐藏活动指示器。我正在考虑使用标志。但我不确定这是否是更好的方法。请帮助我一些更好的方法。

4

3 回答 3

0

一种简单的方法是使用 AFNetworking 库,它会为您处理 NetworkActivityIndi​​cator。您只需要设置 [[AFNetworkActivityIndi​​catorManger sharedManager] setEnabled:YES]; 在您的应用委托中。

否则,保留一个柜台是一个很好的方法。当您启动每个 Web 请求时,只需在成功或失败块中增加计数器并减少计数器。在这些块中,递减计数器后,如果计数器等于 0,则将 [UIApplication sharedApplication] 的 isNetworkActivityIndcatorVisible 属性设置为 NO。

于 2013-06-11T11:50:28.283 回答
0
You need to create a method in AppDelegate.m class

#pragma mark activity indicator view
-(void)showActivityViewer:(NSString* )msg
{
    if (activityView1 == nil)
    {
        [activityView1 release];

        activityView1 = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 320, 580)];
        activityView1.backgroundColor = [UIColor darkGrayColor];
        activityView1.alpha = 0.8;

        UILabel* lblLoading=[[UILabel alloc] initWithFrame:CGRectMake(70, 260, 180, 60)];
        lblLoading.numberOfLines=0;
        lblLoading.text=msg;
        lblLoading.backgroundColor=[UIColor clearColor];
        lblLoading.textAlignment=UITextAlignmentCenter;
        [lblLoading setContentMode:UIViewContentModeBottom];
        lblLoading.textColor=[UIColor whiteColor];
        [activityView1 addSubview:lblLoading];
        [lblLoading release];

        UIActivityIndicatorView *activityWheel = [[UIActivityIndicatorView alloc] initWithFrame: CGRectMake(self.window.bounds.size.width / 2 - 15, self.window.bounds.size.height / 2 - 30, 30, 30)];
        activityWheel.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
        activityWheel.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
                                          UIViewAutoresizingFlexibleRightMargin |
                                          UIViewAutoresizingFlexibleTopMargin |
                                          UIViewAutoresizingFlexibleBottomMargin);
        [activityView1 addSubview:activityWheel];
        [activityWheel release];
    }

    [self.window addSubview: activityView1];

    [[[activityView1 subviews] objectAtIndex:0] setText:msg];
    [[[activityView1 subviews] objectAtIndex:1] startAnimating];
}

每当您需要在整体中显示活动指示器时,请使用此...它会自动管理所有内容

隐藏指标调用此方法,在 AppDelegate.m 类中相同

-(void)hideActivityViewer
{
    [[[activityView1 subviews] objectAtIndex:1] stopAnimating];
    [activityView1 removeFromSuperview];
}
于 2013-06-11T13:30:23.730 回答
0

尝试这个。

在前面添加一行[self.window addSubview: activityView1]

activityView1.tag = 110;

(您也可以设置 110 以外的任何数字)

并编写以下代码以删除:

for(UIView *viewActivityIndicator in [[UIApplication sharedApplication] keyWindow].subviews)
{
    if(viewActivityIndicator.tag == 110)
    {
        [viewActivityIndicator removeFromSuperview];
    }
}

如果没有特殊要求,可以使用一些库而不是管理自己。

一些很好的例子:
1. SVProgressHUD
2. MBProgressHUD

于 2013-06-11T13:42:47.510 回答