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

标签 ios objective-c

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

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

请提出改进​​建议

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/

相关文章:

ios - AdMob SDK 在 iOS 7.1 下编译失败

objective-c - 对象泄露 : Object allocated and stored is not referenced later in this execution

ios - 如何使用 RxSwift 强制点击 UIButton?

iphone - 从 iPhone 应用调用电话

ios - 如何检测我的功能测试在哪个 iOS 设备上运行?

ios - NSUserDefaults 值定期丢失

Javascript 返回不适合 UIWebView 给定宽度和高度的内容/字符串

iphone - applicationWillTerminate 在崩溃时被调用

ios - UITextField 触摸事件在 UIScrollView 中不起作用

iphone - 上标任何字母/数字