作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试搜索,但没有找到任何线索。我还依赖于放下一个按钮,当我尝试在该按钮的功能中设置事件指示器的动画时,它正在设置动画。
只要我在按钮的方法中编写代码,事件指示器讲解器就会工作。
请提出改进建议
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 - 即使在编写 [activityIndicator startAnimating] 之后,事件指示器也没有动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30223236/
我是一名优秀的程序员,十分优秀!