-
28-09-2019 - |
题
我试图获得一个繁忙的图显示在搜索过程,但是它似乎从来没有显示。
我想要实现
- 用户点击搜索的按钮,一旦他们进入了文本中的一个UISearchBar.
- 输入的文本是委派给searchBarSearchButtonClicked.
- 显示一个"忙碌的图",这是一)包含一个UIActivityIndicatorView指示搜索正在进行中。
- 执行搜索与一个网站。
- 搜索完成删除在"忙碌的观"。
什么错误的
搜索过程是工作的现,使用的调试我可以看到移动通过searchBarSearchButtonClicked功能的现但是在"繁忙的图"永远不会出现。搜索需要2-3秒钟,因此在理论上我应该看看我的"繁忙的观点"的出现,对于那些2-3秒钟。
代码试图1- 添加和去除的"繁忙的图"从超景**
- (void)searchBarSearchButtonClicked:(UISearchBar *)activeSearchBar {
[activeSearchBar setShowsCancelButton:NO animated:YES];
[activeSearchBar resignFirstResponder];
[busyView activateSpinner]; //start the spinner spinning
[self.view addSubview:busyView]; //show the BusyView
Search *search = [[Search alloc]init];
results = [search doSearch:activeSearchBar.text];
[busyView deactivateSpinner]; //Stop the spinner spinning
[busyView removeFromSuperview]; //remove the BusyView
[search release]; search = nil;
}
结果的企图1- 它不会出现,但是如果我出评论的removeFromSuperview叫忙图仍然是在屏幕上。
代码试图2- 使用动画
- (void)searchBarSearchButtonClicked:(UISearchBar *)activeSearchBar {
[activeSearchBar setShowsCancelButton:NO animated:YES];
[activeSearchBar resignFirstResponder];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop)];
[busyView activateSpinner]; //start spinning the spinner
[self.view addSubview:busyView]; //show the busy view
[UIView commitAnimations];
Search *search = [[Search alloc]init];
results = [search doSearch:activeSearchBar.text];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop)];
[busyView deactivateSpinner]; //sets stops the spinner spinning
[busyView removeFromSuperview]; //remove the view
[UIView commitAnimations];
[search release]; search = nil;
}
结果的企图2- 有没有看不到的"繁忙的观点"的出现
解决方案
好吧,这是我的解决方案:
- 创建单独的类MyLoadingView.这类应包含显示和隐藏静态的方法。
- 在MyLoadingView构造应手动创建一个图与半透明的黑色的背景和查看里面。将此视为
[[UIApplication sharedApplication] keyWindow]
.隐藏。 [MyLoadingView show]
调用只会带来myLoadingView目前在它的容器:[[[UIApplication sharedApplication] keyWindow] bringSubviewToFront:myLoadingViewSharedInstance];
.也不要忘记,开始查看动画。[MyLoadingView hide]
停止查看动画和隐藏sharedInstanceView.
请注意,需要调用 [MyLoadingView show]
在一个单独的螺纹。
其他提示
UI变化,只有可可生效的下你的时间返回代码控制的运行环。只要你搜索任务的运行,同时在主线,它将阻止执行,包括用户界面更新。
你应该执行耗时的任务异步的背景。有一定数量的选项(NSOperation
, performSelectorInBackground:...
, 大中央调度,...).
不隶属于 StackOverflow