调用 API 时的活动指示器



我想在等待 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];
}

不要在表视图数据源方法中添加活动指标 -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()
}
}

我的详细答案如下

-(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 {
}
}

最新更新