【问题标题】:Activity Indicator while calling API调用 API 时的活动指示器
【发布时间】:2017-08-11 02:30:08
【问题描述】:

我想在等待 API 返回时显示活动指示器。问题是我从 API 获得的所有结果,然后微调器只显示。我想要的结果是在等待 API 调用时,然后微调器将运行。

我在这里调用这个方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
   [self startLoadingSpinner]
   //Calling API...
   [self stopLoadingSpinner]
}

这里是活动指示器的方法

-(void)startLoadingSpinner {
    self.activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 70, 70)];
    self.activityIndicator.opaque = YES;
    self.activityIndicator.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.4f];
    self.activityIndicator.center = self.view.center;
    self.activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    [self.activityIndicator setColor:[UIColor whiteColor]];
    [self.view addSubview:self.activityIndicator];
    [self.activityIndicator startAnimating];
}

这就是我停止活动指示器的方式

-(void)stopLoadingSpinner {
    [self.activityIndicator performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.5];
}

【问题讨论】:

  • Calling API... 代码?
  • 在 numberOfRowsInSection 中调用 API? O_O
  • 提供您的 API 调用代码。
  • 在下面查看我的答案

标签: ios objective-c


【解决方案1】:

不要在 tableview 数据源方法中添加活动指示器 - numberOfRowsInSection

在您进行 API 调用的同一方法中添加这两个调用函数。在 ViewDidLoad、一些生命周期方法动作方法中进行 API 调用。

下面是使用它的例子。

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

NSURL *URL = [NSURL URLWithString:@"http://httpbin.org/get"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];

[self startLoadingSpinner]

NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (error) {
        NSLog(@"Error: %@", error);

    } else {
        NSLog(@"%@ %@", response, responseObject);
    }

    [self stopLoadingSpinner]
}];
[dataTask resume];

斯威夫特

func makeAPIRequest(to endPoint: String) {

    // here you can showActivetyIndicator start progressing here
    self.startLoadingSpinner()   
    Alamofire.request(endPoint).responseJSON{ response in
        if let value = response.result.value {
            let responseInJSON = JSON(value)
            self._responseInJSON = responseInJSON
        }
       // here you can hide Your ActivetyIndicator here
      self.stopLoadingSpinner()
    }
}

【讨论】:

  • 但是 OP 想要 objC bruh。
  • 是的。请稍等。我正在做出改变
  • 编辑了答案@GeneCode。谢谢;-)
  • 在进行 API 调用时,如果您正在加载微调器,则意味着用户在一段时间内不会使用 UI。如果我们仍然希望用户与应用程序进行交互?那么我不建议在主线程上加载
  • nayem 说的是“[self stopLoadingSpinner]”应该在主线程上调用。因为您的代码现在在后台进程 (NSURLSession) 上更新 UI
【解决方案2】:

下面是我的详细回答

-(void)simpleGetResponse{
    @try {
            //Call the Activity Indicator show method here
            [self startLoadingSpinner];
            NSString *strURL = @"Your URL";
            NSURL *urlStr = [NSURL URLWithString:strURL];
            NSMutableURLRequest *mutaURL = [NSMutableURLRequest requestWithURL:urlStr];
            [mutaURL setHTTPMethod:@"GET"];
            NSURLSession *session = [NSURLSession sharedSession];
            NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:mutaURL completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
                                              {
                                                  NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
                                                  if(httpResponse.statusCode == 200)
                                                  {
                                                      NSError *parseError = nil;
                                                      id response = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
                                                      if(response != nil){
                                                          if([response isKindOfClass:[NSDictionary class]]){
                                                              NSLog(@"response is in dictionary format %@",response);
                                                              NSDictionary *dictRes = [response copy];
                                                              NSLog(@"The dictRes is - %@",dictRes);
                                                          }
                                                          else{
                                                              NSLog(@"response is in array format %@",response);
                                                              NSDictionary *arrRes = [response copy];
                                                              NSLog(@"The arrRes is - %@",arrRes);
                                                          }
                                                          dispatch_async(dispatch_get_main_queue(), ^{
                                                             //Call the Activity Indicator hidden method inside the dispatch_main_queue method
                                                             [self stopLoadingSpinner]
                                                             [yourTableView reloadData];
                                                          });
                                                      }
                                                  }
                                                  else
                                                  {
                                                      NSLog(@"Error");
                                                  }
                                              }];
            [dataTask resume];
    }
    @catch (NSException *exception) {
        NSLog(@"%@", [exception description]);
    }
    @finally {
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    • 2011-06-28
    • 2012-06-08
    • 2010-11-08
    相关资源
    最近更新 更多