UIActivityIndi​​catorView 不停止

UIActivityIndicatorView not stopping

我是 iOS 开发的新手。今天学习了什么是UIActivityIndi​​catorView。 现在,我正在构建一个项目,其中有一个 table 视图,我使用 JSON 解析填充了 table。现在我添加了一个 activity 指示器,它会旋转到那个时间,直到 table 被填充。 我已经启动了 activity 指标,但它并没有停止。你能告诉我我哪里错了吗?提前致谢。 这是我的代码。

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self fetchData];
self.mySpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
_mySpinner.hidden = NO;

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (void) fetchData  {

[_mySpinner startAnimating];

NSString *strURL = [NSString stringWithFormat:@"http://api.kivaws.org/v1/loans/search.json?status=fundraising"];
NSURL *url = [NSURL URLWithString:strURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn) {
    _webData = [NSMutableData data];
}
else{
    //error
}

}

#pragma mark Url connection Delegate

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// A response has been received, this is where we initialize the instance var you created
// so that we can append data to it in the didReceiveData method
// Furthermore, this method is called each time there is a redirect so reinitializing it
// also serves to clear it
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Append the new data to the instance variable you declared
[_webData appendData:data];

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

// The request is complete and data has been received
// You can parse the stuff in your instance variable now
// self.data parse

NSDictionary *dict= [NSJSONSerialization JSONObjectWithData:self.webData options:kNilOptions error:nil];
self.arrDetail = [dict valueForKey:@"loans"];

[self.mySpinner stopAnimating];
self.mySpinner.hidden = YES;

[self.parserTable reloadData];

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// The request has failed for some reason!
// Check the error var

}

#pragma mark Table View Delegates


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];


NSDictionary *locationDict = [[self.arrDetail objectAtIndex:indexPath.row]valueForKey:@"location"];

UILabel *lbl1 = (UILabel*)[cell.contentView viewWithTag:1];
lbl1.text = [[self.arrDetail objectAtIndex:indexPath.row]valueForKey:@"name"];

UILabel *lbl2 = (UILabel*)[cell.contentView viewWithTag:2];
lbl2.text = [locationDict valueForKey:@"country"];


return cell;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return [self.arrDetail count];

}


@end

首先初始化您的微调器,然后调用您的连接方法,如下所示。

...

- (void)viewDidLoad {
    [super viewDidLoad];
    self.mySpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    _mySpinner.hidden = NO;
    _mySpinner.center = self.view.center;
    [self fetchData];
}

...

开始 Activity 指标:

[cell.indicater startAnimating];

开始 Activity 指标:

[cell.indicater stopAnimating];

同时设置属性,如屏幕截图所示。

由于您正在将 UIActivityIndicatorView 的新实例分配给出口 属性,因此对 InterfaceBuilder 中的集合的引用丢失了。

只需删除:self.mySpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];

解决方案

从代码中删除这一行

self.mySpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]

问题

您还没有出示 mySpinner 的 属性 声明。但是从你的代码我可以看出它是一个 IBOutlet 因为你已经创建了一个新的 UIActivityIndicatorView 实例并且没有将它添加为子视图并且你仍然可以在你的视图上看到一个 activity 指示器(因为它出现并且永不停止动画)。

它不停止动画的原因是您在 IBOutlet 上调用 [_mySpinner startAnimating];。然后当你说

时,你创建了一个 UIActivityIndicatorView 的新实例

self.mySpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]

现在,您在 mySpinner 上调用的任何方法都将在 activity 指标上被调用,该指标不在您的视图中,而是在您创建的视图中,因为您在创建时就失去了对它的引用一个新的 UIActivityIndicatorView.

另外,既然你是新来的。我建议你尽可能使用self.mySpinner,不要交替使用self_,因为两者都是根据需要使用的。的原因超出了您的问题范围。