我试图在搜索过程中显示繁忙的视图,但它似乎从未显示。
我想要达到的目标
- 用户在 UISearchBar 中输入文本后单击搜索按钮。
- 输入的文本被委托给 searchBarSearchButtonClicked。
- 显示一个“忙碌视图”,它是一个 UIView,其中包含一个 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 的结果 -没有看到“忙碌视图”出现