gpt4 book ai didi

ios - 即使在编写 [activityIndi​​cator startAnimating] 之后,事件指示器也没有动画

转载 作者:行者123 更新时间:2023-11-28 18:57:56 24 4
gpt4 key购买 nike

我尝试搜索,但没有找到任何线索。我还依赖于放下一个按钮,当我尝试在该按钮的功能中设置事件指示器的动画时,它正在设置动画。

只要我在按钮的方法中编写代码,事件指示器讲解器就会工作。

请提出改进​​建议

NSString *home= @"http://kivihealth.com/api/";
NSString *apiType = @"patientapi/";
NSString *apiMethod = @"authenticatepatient?";

NSMutableString *url = [[NSMutableString alloc]initWithString:home];
[url appendString:apiType];
[url appendString:apiMethod];

NSString *temp=[NSString stringWithFormat:@"email=%@&password=%@",self.emailInputted.text,self.PasswordInputted.text];
[url appendString:temp];

// PROBLEM - NOT WORKING
[self.activityIndicator startAnimating];

NSURL *finalUrl = [NSURL URLWithString:url];
NSData *data = [NSData dataWithContentsOfURL:finalUrl];
NSError *error;

[self.activityIndicator stopAnimating];

if(data == nil)
{
UIAlertView *invalidDetailsAlert = [[UIAlertView alloc]initWithTitle:@"SIGN IN FAILED" message:@"Netwok error" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[invalidDetailsAlert show];
}
else
{
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSDictionary *da = json[@"data"];
NSString *message = da[@"message"];
if ([message isEqualToString:@"Valid Details"])
{
//NSLog(@"Inside Valid");
//NSLog(@"%@",da);
Patient *defaultPatient = [[Patient alloc]init];
defaultPatient=[defaultPatient initWithRegId:da[@"regid"] city:da[@"city"] area:da[@"area"] mobile:da[@"mobile"] landline:da[@"landline"] dob:da[@"dob"] sex:da[@"sex"] name:da[@"name"] chronicDiseases:da[@"chronicdiseases"] aboutHealth:da[@"abouthealth"] email:self.emailInputted.text];
[defaultPatient saveAsDefaultUser];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
AfterLoginViewController *afterLoginViewController = (AfterLoginViewController *)[storyboard instantiateViewControllerWithIdentifier:@"AfterLoginViewController"];
[self presentViewController:afterLoginViewController animated:YES completion:nil];
}
else
{
UIAlertView *invalidDetailsAlert = [[UIAlertView alloc]initWithTitle:@"SIGN IN FAILED" message:message delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[invalidDetailsAlert show];
}
}

最佳答案

自从我最初发布这篇文章以来,编辑发生了很多变化

动画的执行必须在主线程上进行,而加载内容的过程必须在另一个线程上进行。

如果您想在一个线程上运行所有内容,只要执行/运行循环未完成,主线程(UI 运行所在的线程)就会被阻塞。

swift 3

let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)

// put the activityIndicator to use somewhere or use an IBOutlet

activityIndicator.startAnimating()

DispatchQueue.global(qos: .background).async {

// do your stuff

DispatchQueue.main.async {
activityIndicator.stopAnimating()
}
}

不幸的是,这实际上不再适用于网络调用,因为它的性质已经转移到 URLSession 中,它看起来像这样:

let url = URL(with: "http://google.com")
let urlRequest = URLRequest(string: url)
let task = URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in
// do some parsing and verify/validate your response is not an error
// eg the response.statusCode could be helpful to see if an error occurred
...
DispatchQueue.main.async {
// report back to ViewController
}
}
task.resume()

旧的解决方案:

这是一个你可以做的例子

[self.activityIndicator startAnimating];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *finalUrl = [NSURL URLWithString:url];
NSData *data = [NSData dataWithContentsOfURL:finalUrl];
NSError *error;
// do stuff
dispatch_async(dispatch_get_main_queue(), ^{
[self.activityIndicator stopAnimating];
// UI Updates
});
});

在我看来,整个代码看起来应该有所不同,请尝试使用 AFNetworking更好地组织您的网络代码。阅读页面上的文档并考虑使用 Cocoapods

关于ios - 即使在编写 [activityIndi​​cator startAnimating] 之后,事件指示器也没有动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30223236/

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