在调用服务器获取数据期间看不到 UIActivityIndicatorView
Posted
技术标签:
【中文标题】在调用服务器获取数据期间看不到 UIActivityIndicatorView【英文标题】:Cant see the UIActivityIndicatorView during Call to Server for data 【发布时间】:2012-03-26 09:40:14 【问题描述】:我正在编写一个应用程序,该应用程序正在调用服务器以获取数据以在应用程序启动时向用户显示,对服务器的调用是同步调用,我想向用户显示 UIActivityIndicatorView,但我看不到它虽然我在新线程中激活 UIActivityIndicatorView,但听到的是代码:
- (void)viewDidLoad
[super viewDidLoad];
spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
spinner.frame = CGRectMake(50, 50, 50, 50);
[self.tableView addSubview:spinner];
[self.tableView bringSubviewToFront:spinner];
singeltoneData *sing = [singeltoneData sharedInstance];
firstTimeSearch = YES;
firstTimeSearchClick = YES;
NSNumber *num = [[NSNumber alloc]initWithInt:-1];
[NSThread detachNewThreadSelector:@selector(spin:) toTarget:self withObject:nil];
[self getData:num];
filterCalls = [[sing.globalCallsDitionary objectForKey:@"Calls"]mutableCopy];
allCalls = [[sing.globalCallsDitionary objectForKey:@"Calls"]mutableCopy];
callsDetails = [[sing.globalCallsDitionary objectForKey:@"CallDetails"]mutableCopy];
filteredCallsDetails = [[sing.globalCallsDitionary objectForKey:@"CallDetails"]mutableCopy];
#if defined(__IPHONE_5_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_5_0
if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)])
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"top.png"] forBarMetrics:UIBarMetricsDefault];
#endif
[self buildBar];
woman = [UIImage imageNamed:@"woman.png"];
接下来我的自旋函数是这样的
/************************************************************/
/* Spinner */
/************************************************************/
- (void) spin:(id)data
[spinner startAnimating];
我也是从刷新数据调用服务器调用它:
- (void)activateActions:(id)sender
[NSThread detachNewThreadSelector:@selector(spin:) toTarget:self withObject:nil];
singeltoneData *sing = [[singeltoneData sharedInstance]autorelease];
[allCalls removeAllObjects];
[callsDetails removeAllObjects];
[filterCalls removeAllObjects];
[filteredCallsDetails removeAllObjects];
NSNumber *num = [[NSNumber alloc]initWithInt:-1];
[self getData:num];
filterCalls = [[sing.globalCallsDitionary objectForKey:@"Calls"]mutableCopy];
filteredCallsDetails = [[sing.globalCallsDitionary objectForKey:@"CallDetails"]mutableCopy];
allCalls = [[sing.globalCallsDitionary objectForKey:@"Calls"]mutableCopy];
callsDetails = [[sing.globalCallsDitionary objectForKey:@"CallDetails"]mutableCopy];
[self.tableView reloadData];
我还是没有看到微调器
有什么帮助吗?
【问题讨论】:
【参考方案1】:[NSThread detachNewThreadSelector:@selector(spin:) toTarget:self withObject:nil];
- (void) spin:(id)data
[spinner startAnimating];
根据上述情况,您正在为不同线程上的微调器设置动画..但所有 UI 更新都应在主线程上完成..所以这可能是您看不到活动动画的原因
【讨论】:
以上是关于在调用服务器获取数据期间看不到 UIActivityIndicatorView的主要内容,如果未能解决你的问题,请参考以下文章
Flutter Hooks 使用 useEffect 获取数据 - 在构建期间调用 setState() 或 markNeedsBuild()
如何在另一个插入存储过程期间调用一个插入存储过程并获取其返回的ID?