我试图在搜索过程中显示一个忙碌的视图,但它似乎永远不会显示。
我想要实现的目标
错误
搜索过程运行正常,使用调试器我可以看到它在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调用,则Busy View仍然在屏幕上。
代码尝试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 - 未看到“忙碌视图”出现
答案 0 :(得分:1)
嗯,这是我的解决方案:
[[UIApplication sharedApplication] keyWindow]
。隐藏它。[MyLoadingView show]
调用只是将myLoadingView对象放在它的容器前面:[[[UIApplication sharedApplication] keyWindow] bringSubviewToFront:myLoadingViewSharedInstance];
。另外,不要忘记启动activityview动画。[MyLoadingView hide]
停止活动视图动画并隐藏sharedInstanceView。请注意,您需要在单独的帖子中调用[MyLoadingView show]
。
答案 1 :(得分:0)
Cocoa中的UI更改仅在下次代码将控制权返回到运行循环时生效。只要您的搜索任务在主线程上同步运行,它就会阻止执行,包括UI更新。
您应该在后台异步执行耗时的任务。这有很多选项(NSOperation
,performSelectorInBackground:...
,Grand Central Dispatch,...)。