我想在搜索過程中顯示繁忙的視圖,但它似乎永遠不會顯示。iPhone忙碌視圖
我所試圖實現對搜索按鈕
- 用戶點擊,一旦他們已經進入一個UISearchBar的文本。
- 輸入的文本被委託給searchBarSearchButtonClicked。
- 顯示「忙視圖」,它是一個包含UIActivityIndicatorView的UIView,用於指示正在進行搜索。
- 執行與網站通信的搜索。
- 完成搜索後,請刪除「繁忙視圖」。
哪些錯誤
搜索過程做工精細,使用調試器,我可以看到它通過函數在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的結果 -沒有看到 「忙視圖」 顯示
謝謝Kovpas,正是我在找的。我創建了一個靜態類,它有一個線程函數調用,可以像你說的那樣加載視圖。 – woot586 2010-11-04 21:22:03