在我的代码中,我初始化了一个UIActivityIndicator,并将其添加为子视图,但它从未出现过。出了什么问题?
-(void) update {
UIActivityIndicatorView *activityView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityView.center=self.view.center;
[self.view addSubview:activityView];
[activityView startAnimating];
/** Stuff to access to server, this stuff works correctly.*/
[activityView stopAnimating];
}
答案 0 :(得分:0)
由于您starting
和stoping
活动与runloop
相同,因此更新UI
不会发生变化。您正在全力以赴server stuff
在同一个synchronously
上阻止runLoop
并且没有给activity
指标启动时间。您应该在下一个runLoop
启动服务器内容,这样活动指标就会有时间开始使用performSelector
,延迟时间为0.0,它会启动activity indicator
而不是立即执行服务器内容runloop
而不是停止activityInddicator
-(void) update {
UIActivityIndicatorView *activityView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityView.center=self.view.center;
[self.view addSubview:activityView];
[activityView startAnimating];
//Use performSelector with 0.0 so activity indicator starts
[self performSelector:@selector(thisWillStartAtNextRunLoop) withObject:nil afterDelay:0.0];
}
-(void)thisWillStartAtNextRunLoop{
/** Stuff to access to server, this stuff works correctly.*/
[activityView stopAnimating];
}
修改在main thread
执行,因为所有的UI更新都必须在main thread
上
试试
-(void) update {
dispatch_async(dispatch_get_main_queue(), ^{
UIActivityIndicatorView *activityView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityView.center=self.view.center;
[self.view addSubview:activityView];
[activityView startAnimating];
});
/** Stuff to access to server, this stuff works correctly.*/
}