【问题标题】:What is the proper way to display a loading indicator while getting data from a URL从 URL 获取数据时显示加载指示器的正确方法是什么
【发布时间】:2015-07-29 19:03:46
【问题描述】:

我对 Objective-c 有点陌生,我正在开发一个新闻 iOS 应用程序,该应用程序使用 JSON 解析从 url 获取其所有内容,我为此使用 AFNetworking,这是我做了:

- (void)getContents
{
    NSString *urlString = @"http://some-url-that-has-json-output/";
    urlString = [urlString stringByAppendingString:self.articleId];
    NSLog(@"The call url is: %@",urlString);
    NSURL *url = [NSURL URLWithString:urlString];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    //AFNetworking asynchronous url request
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
                                         initWithRequest:request];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"The JSON data is: %@", responseObject);
        jsonContents = [responseObject objectForKey:@"article"];
        [self LoadStructure];
    } failure:nil];
    [operation start];
}

现在使用这种方法可以很好地加载数据。

我的问题:如何在获取数据时显示加载指示器(可能是 GIF)?上面的这种方法是从 url 获取数据的正确或最佳方法吗?

【问题讨论】:

    标签: ios objective-c json afnetworking


    【解决方案1】:

    您可以使用 iOS 的默认加载指示器(UIActivityIndi​​cator)。您应该在完成块之前开始对其进行动画处理,并且应该隐藏在成功和失败块中。

    你应该创建一个使用指标作为类变量的方法:

    indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    indicator.hidesWhenStopped = YES;
    indicator.frame = CGRectMake(35, 15, 30, 30);
    [self.view addSubview:indicator];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    //AFNetworking asynchronous url request
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
                                         initWithRequest:request];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];
    [indicator startAnimating];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"The JSON data is: %@", responseObject);
    

    // 停止:

    [indicator stopAnimating];
        jsonContents = [responseObject objectForKey:@"article"];
        [self LoadStructure];
    } failure::^(AFHTTPRequestOperation *operation, NSError *error){
        [indicator stopAnimating];
    }];
    [operation start];
    

    【讨论】:

    • 我做了,它没有显示,我还在[self.view addSubview:indicator]之后添加了这一行[indicator startAnimating]
    • 我正在使用滚动视图,这有关系吗?
    • 请检查更改指标框架
    • 检查在 ((AppDelegate *)[UIApplication sharedApplication].delegate).window 上添加指示器而不是 self.view
    • 我成功了!谢谢,我在情节提要中手动添加了它,它现在可以工作了,但是我必须在我停止动画后隐藏它吗?
    【解决方案2】:

    UIActivityIndicatorView 拖放到您的XIB 视图中并将其与IBOutlet 连接。

    .h 文件

    @property(nonatomic, strong)IBOutlet UIActivityIndicatorView * activityIndicator;
    

    将 UIActivity 指示器视图添加到您的视图中。在之前展示它

    NSString *urlString = @"http://some-url-that-has-json-output/";

    使用:[self.activityIndicator startAnimating];

    并在完成块和失败块内停止它

    使用:[self.activityIndicator stopAnimating];

    【讨论】:

    • 你能发布整个更新的方法吗?完成块在哪里?
    • setCompletionBlockWithSuccess..这是你的完成块。 NSLog(@"The JSON data is: %@", responseObject);之前的停止指标
    • 所以setCompletionBlockWithSuccess 是一个单独的方法,我必须在上面的方法中调用它(在nslog 之前)?
    • 这个[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 代码将在您从服务器成功接收数据时运行。它是 AFNetworking 提供的默认块。
    • 啊好吧抱歉,让我测试一下:)
    猜你喜欢
    • 2018-05-27
    • 2016-08-10
    • 2017-09-14
    • 2010-09-10
    • 1970-01-01
    • 2021-10-09
    • 2020-06-10
    • 2021-07-06
    • 2014-03-27
    相关资源
    最近更新 更多