Ios UIActivityIndicator视图未停止

Ios UIActivityIndicator视图未停止,ios,objective-c,uiactivityindicatorview,Ios,Objective C,Uiactivityindicatorview,我对iOS开发完全是新手。今天我学习了什么是UIActivityIndicatorView。 现在,我正在构建一个项目,其中有一个表视图,我使用JSON解析填充了表。现在,我添加了一个活动指示器,它将一直旋转到表被填充为止。 我已启动活动指示器,但它不会停止。你能告诉我哪里错了吗?提前谢谢。 这是我的密码 #import "ViewController.h" @interface ViewController () @end @implementation ViewController

我对iOS开发完全是新手。今天我学习了什么是UIActivityIndicatorView。 现在,我正在构建一个项目,其中有一个表视图,我使用JSON解析填充了表。现在,我添加了一个活动指示器,它将一直旋转到表被填充为止。 我已启动活动指示器,但它不会停止。你能告诉我哪里错了吗?提前谢谢。 这是我的密码

#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

首先初始化微调器,然后像下面那样调用连接方法


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


…要启动活动指示器,请执行以下操作:

[cell.indicater startAnimating];
[cell.indicater stopAnimating];

要启动活动指示器,请执行以下操作:

[cell.indicater startAnimating];
[cell.indicater stopAnimating];
还可以设置如屏幕截图所示的属性。
启动活动指示器:

[cell.indicater startAnimating];
[cell.indicater stopAnimating];

要启动活动指示器,请执行以下操作:

[cell.indicater startAnimating];
[cell.indicater stopAnimating];
还可以设置如屏幕截图所示的属性。

由于您正在将UIActivityIndicatorView的新实例分配给outlet属性,因此对InterfaceBuilder中设置的实例的引用将丢失


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

由于您正在将UIActivityIndicatorView的新实例分配给outlet属性,因此对InterfaceBuilder中设置的实例的引用将丢失

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

解决方案

从代码中删除这一行

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

问题

您尚未显示mySpinner的属性声明。但是,从您的代码中,我可以看出它是一个IBOutlet,因为您已经创建了UIActivityIndicatorView的新实例,并且没有将其添加为子视图,并且您仍然可以在视图上看到活动指示器,因为它出现并且从未停止过动画

它没有停止动画制作的原因是你称之为[_myspinnerstartanimating];在你的IBOutlet上。然后创建UIActivityIndicatorView的新实例

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

现在,无论您在mySpinner上调用什么方法,都会在活动指示器上调用,该指示器不在您的视图中,而是您创建的,因为您在创建新UIActivityIndicatorView时丢失了对它的引用

还有,因为你是新来的。我建议您尽可能使用self.mySpinner,而不要互换使用self和u,因为两者都将根据需要使用。原因超出了您问题的范围。

解决方案

从代码中删除这一行

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

问题

您尚未显示mySpinner的属性声明。但是,从您的代码中,我可以看出它是一个IBOutlet,因为您已经创建了UIActivityIndicatorView的新实例,并且没有将其添加为子视图,并且您仍然可以在视图上看到活动指示器,因为它出现并且从未停止过动画

它没有停止动画制作的原因是你称之为[_myspinnerstartanimating];在你的IBOutlet上。然后创建UIActivityIndicatorView的新实例

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

现在,无论您在mySpinner上调用什么方法,都会在活动指示器上调用,该指示器不在您的视图中,而是您创建的,因为您在创建新UIActivityIndicatorView时丢失了对它的引用


还有,因为你是新来的。我建议您尽可能使用self.mySpinner,而不要互换使用self和u,因为两者都将根据需要使用。原因超出了您的问题范围。

在何处将微调器添加到视图?是否在interface builder?的tableview中添加了UIActivityIndicatorView。没错。在您发布的代码中,您正在代码中创建UIActivityIndicatorView,但从未将其添加到视图中。如果mySpinner是在interface builder中连接的IBOutlet,则无需在代码中创建微调器。我已在interface builder中连接了IBOutlet。他们为什么要将UIActivityIndicatorView的新实例分配给该出口?只需删除self.mySpinner=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];它会起作用你在哪里向视图添加微调器?是否在interface builder?的tableview中添加了UIActivityIndicatorView。没错。在您发布的代码中,您正在代码中创建UIActivityIndicatorView,但从未将其添加到视图中。如果mySpinner是IBOutlet connect

在interface builder中编辑,则无需在代码中创建微调器。我已在interface builder中连接了IBOutlet。他们为什么要将UIActivityIndicatorView的新实例分配给该插座?只需删除self.mySpinner=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];它会工作指示器正在旋转,但填充表后它不会停止。如果已连接Interface Builder,则可以删除微调器分配行。在您的情况下,您有两个不同的微调器,一个位于Interface Builder,并在fetchData方法处开始旋转,然后创建另一个不在屏幕上的微调器。最后调用stopAnimating,但此选择器转到invisible spinner,可见spinner继续旋转我想知道在填充表时在何处编写方法[\u myspinner stopAnimating]以停止指示器。只需删除self.myspinner=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];行它应该可以工作指示器正在旋转,但在填充表后它不会停止。如果已连接Interface Builder,则可以删除微调器分配行。在您的情况下,您有两个不同的微调器,一个位于Interface Builder并在fetchData方法处开始旋转,然后创建另一个不在屏幕上的微调器。最后调用stopAnimating,但此选择器转到invisible spinner,可见spinner继续旋转我想知道在填充表时在何处编写方法[\u myspinner stopAnimating]以停止指示器。只需删除self.myspinner=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];行,应该行,谢谢。我已经意识到我的错误。我只是不知道为什么人们会否决我的问题。:'我的名声还在继续下降大多数情况下,这是因为问题是缺少代码,您正在显示您试图自己解决问题。仅仅指定需求而不做任何尝试可能会被视为是一个令人皱眉的代码请求。非常感谢。我已经意识到我的错误。我只是不知道为什么人们会否决我的问题。:'我的名声还在继续下降大多数情况下,这是因为问题是缺少代码,您正在显示您试图自己解决问题。仅仅指定需求而不做任何尝试可能会被视为是一个被拒绝的代码请求。您是否理解了问题,并看完了代码?您是否理解了问题,并看完了代码?