当我想要时,UIActivityIndi​​catorView不会停止

时间:2013-12-30 06:08:05

标签: ios objective-c uiactivityindicatorview

我需要在调用WebService时显示UIActivityIndicatorView。但是,即使在我收到来自Web服务的响应后,活动指示器仍然显示。它在收到回复后5-6秒后停止。如何在我收到回复时停止?

这是我的代码:(配置UIActivityIndi​​catorView)并调用我的webservice:

loadView = [[UIView alloc] initWithFrame:self.view.bounds];
            loadView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.5];

            //UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] init];
            //[second.loadingView addSubview:activityView];
            //activityView.center = second.loadingView.center;

            //[second.view addSubview:second.loadingView];

            activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
            [loadView addSubview:activity];

            activity.center = loadView.center;
            [self.view addSubview:loadView];
            [self.view bringSubviewToFront:loadView];
            activity.hidesWhenStopped = YES;
            [activity setHidden:NO];

            //[activity performSelectorInBackground: @selector(startAnimating) withObject: nil];

            [activity startAnimating];

            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                [self callRegisterWebService:self.userFname lastName:self.userLName email:self.userEmail];
            });

我正在finally区块停止动画。

-(void)callRegisterWebService:(NSString *)fname lastName:(NSString *)lName email:(NSString *)email
{

    NSString *serviceURL = [NSString stringWithFormat:@"https:abcdefghi..."];

    @try {
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:serviceURL]];
        NSURLResponse *serviceResponse = nil;
        NSError *err = nil;

        NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&serviceResponse error:&err];
        NSMutableDictionary *parsedData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&err];

        if(!parsedData)
        {
            NSLog(@"data not parsed");
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ERROR" message:@"Problem in Network. Please Try Again!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];

        }
        else
        {

            NSString *status = [parsedData objectForKey:@"Status"];            
            if([status isEqualToString:@"Success"])
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NULL message:@"Authentication Token Has Been Sent To Your Email-ID!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
                [alert show];

                NSString *uniqueNumber = [parsedData objectForKey:@"UniqueNum"];
                [self saveEmailAndUniqueNumberToDatabase:fname lastName:lName Email:email Number:uniqueNumber];
            }

            else if([status isEqualToString:@"Failed"])
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Not An Authorized User" message:@"Please Contact Admin To Get Access" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
                [alert show];

            }

            else
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ERROR" message:@"Problem in Network. Please Try Again!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
                [alert show];

            }
        }
    }

    @catch (NSException *exception)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NULL message:@"Problem In Network Connection. Please Try Again!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];

    }
    @finally {

        [activity stopAnimating];
        [loadView setHidden:YES];

    }
}

1 个答案:

答案 0 :(得分:1)

您的代码中有2个问题:

  1. 您正在从后台线程操作UI组件,从不这样做。使用主线程进行UI操作
  2. 您在finally子句中编写了活动指示器功能,因此只有在执行try子句中的所有语句后才会隐藏它
  3. 更改您的方法,如:

    - (void) hideActivity
    {
       dispatch_async(dispatch_get_main_queue(), ^{
            [activity stopAnimating];
            [loadView setHidden:YES];
            activity = nil;
            loadView = nil;
       });
    }
    
    -(void)callRegisterWebService:(NSString *)fname lastName:(NSString *)lName email:(NSString *)email
    {
    
        NSString *serviceURL = [NSString stringWithFormat:@"https:abcdefghi..."];
        NSString *message    = @"";
        NSString *title      = @"";
    
        @try
        {
            NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:serviceURL]];
            NSURLResponse *serviceResponse = nil;
            NSError *err = nil;
    
            NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&serviceResponse error:&err];
            NSMutableDictionary *parsedData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&err];
            [self hideActivity];
    
            if(!parsedData)
            {
                message = @"Problem in Network. Please Try Again!";
                title   = @"ERROR";
            }
            else
            {
    
                NSString *status = [parsedData objectForKey:@"Status"];            
                if([status isEqualToString:@"Success"])
                {
                    message = @"Authentication Token Has Been Sent To Your Email-ID!";
                    title   = nil;
                    NSString *uniqueNumber = [parsedData objectForKey:@"UniqueNum"];
                    [self saveEmailAndUniqueNumberToDatabase:fname lastName:lName Email:email Number:uniqueNumber];
                }
    
                else if([status isEqualToString:@"Failed"])
                {
                    message = @"Please Contact Admin To Get Access";
                    title   = @"Not An Authorized User";
                }
    
                else
                {
                    message = @"Problem in Network. Please Try Again!";
                    title   = @"ERROR";
                }
            }
        }
        @catch (NSException *exception)
        {
            if (activity != nil && loadView != nil)
            {
                [self hideActivity];
            }
            message = @"Problem In Network Connection. Please Try Again!";
            title   = nil;
        }
        @finally
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];
        }
    }