gpt4 book ai didi

ios - UIActivityIndi​​catorView 在我想要的时候不停止

转载 作者:行者123 更新时间:2023-11-29 03:23:05 25 4
gpt4 key购买 nike

我需要在调用 WebService 时显示 UIActivityIndi​​catorView。但是,即使在我收到来自 Web 服务的响应后,事件指示器仍会继续显示。它仅在我收到响应后 5-6 秒后停止。如何让它在我收到回复时停止?

这是我的代码:(配置 UIActivityIndi​​catorView)并调用我的网络服务:

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 block 中停止动画。

-(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];

}
}

最佳答案

您的代码中有 2 个问题:

  1. 您正在从后台线程操作 UI 组件,切勿这样做。使用主线程进行 UI 操作
  2. 您在finally子句中编写了事件指示器功能,因此只有在执行try子句中的所有语句后它才会被隐藏

像这样改变你的方法:

- (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];
}
}

关于ios - UIActivityIndi​​catorView 在我想要的时候不停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20834255/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com