[英]UISearchBar cancel button crash
我有一個UITableView,在didSelectRowAtIndexPath上推送另一個UITableView進行搜索...
LocationSearchViewController *locationSearchViewController = [[LocationSearchViewController alloc] initWithNibName:@"LocationSearchViewController" bundle:nil];
locationSearchViewController.delegate = self;
UITableViewCell *cell = [myTableView cellForRowAtIndexPath:indexPath];
locationSearchViewController.defaultLocation = cell.detailTextLabel.text;
[[self navigationController] pushViewController:locationSearchViewController animated:YES];
[locationSearchViewController release];
搜索視圖包含一個UISearchBar,並啟用了取消按鈕。 如果用戶點擊取消按鈕,應用程序將崩潰並顯示objc_exception_throw。 控制台顯示......
2011-02-06 22:05:43.960 JetLogger [2381:207] *斷言失敗 - [UISearchDisplayController setActive:animated:],/ SourceCache / UIKit_Sim / UIKit-1447.6.4 / UISearchDisplayController.m:589
但是,如果用戶點擊表格視圖中的項目(搜索結果),我將關閉搜索視圖,並且沒有崩潰。 我不明白為什么差異,因為我用相同的代碼彈出搜索視圖...
在LocationSearchViewController中......
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self.delegate respondsToSelector:@selector(locationSearchViewDidDismiss:withLocation:)]) {
[self.delegate locationSearchViewDidDismiss:self withLocation:[tableView cellForRowAtIndexPath:indexPath].textLabel.text];
}
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
if ([self.delegate respondsToSelector:@selector(locationSearchViewDidDismiss:withLocation:)]) {
[self.delegate locationSearchViewDidDismiss:self withLocation:@"No results found"];
}
}
在代表......
- (void)locationSearchViewDidDismiss:(LocationSearchViewController *)controller withLocation:(NSString*)location{
if((location != @"") && (location != @"No results found")){
//update the table data
[myTableView reloadData];
}
[self.navigationController popViewControllerAnimated:YES];
}
任何幫助表示贊賞。
約翰
因為我們彈出viewController,搜索欄會失去焦點。 這會導致調用setActive,但在發生這種情況時,搜索欄已全部解除分配。 所以我們需要在彈出之前將active設置為no。
因此,一個簡單的解決方法是:
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[self.searchDisplayController setActive:NO animated:NO];
[self.navigationController popViewControllerAnimated:YES];
}
我認為提供的答案是不完美的,因為它們間接解決了問題,即在解除分配的委托上調用searchDisplayControllerDidEndSearch:
調用[self.searchDisplayController setActive:NO animated:NO]
可能在searchBarCancelButtonClicked:
工作,但它會在其他地方失敗,例如在searchBarTextDidEndEditing:
相反,只需在彈出控制器之前直接清除委托: self.searchDisplayController.delegate = nil;
這將確保不會在解除分配的實例上調用委托方法。
我遇到了同樣的問題。 XCode 4 IOS 4.3
對我來說問題是只有當焦點在搜索欄內時我才發現一些可能有用的解決方法。
這是代碼:
static BOOL _cancelBtnClicked = NO;
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
Log(@"Enter");
if(searchDisplayController.active){
_cancelBtnClicked = YES;
}else{
[self back];
}
}
- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller{
Log(@"Enter");
if(_cancelBtnClicked){
_cancelBtnClicked = NO;
[self back];
}
}
-(void)back{
[self.navigationController popViewControllerAnimated:YES];
}
我有同樣的問題。
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[self.searchDisplayController setActive:NO animated:NO];
[self.navigationController popViewControllerAnimated:YES];
}
在我的情況下沒有解決它。 相反,我在另一個地方調用[self.searchDisplayController setActive:NO animated:NO],使之前被調用。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// ....
[self.searchDisplayController setActive:NO animated:NO];
}
斯威夫特版
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
self.searchDisplayController?.active = false
self.dismissViewControllerAnimated(true, completion: nil)
}
我剛遇到這個問題。
當我使用popViewControllerAnimated
關閉包含搜索內容的viewcontroller時,我收到此錯誤。 如果您以模態方式呈現相同的viewcontroller,並使用modal dismiss函數關閉,則此斷言也不會觸發。
要解決此問題,我必須添加(使用您的代碼作為示例)
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
[self.searchController setActive:NO animated:NO];
if ([self.delegate respondsToSelector:@selector(locationSearchViewDidDismiss:withLocation:)]) {
[self.delegate locationSearchViewDidDismiss:self withLocation:@"No results found"];
}
}
這樣,searchcontroller在調用它想要的任何內部函數之前是不活動的。 感覺這是一個4.2 sdk的錯誤,但我需要用新的4.3克種子來檢查這個。
你找到了其他解決方案嗎?
我寧願不使用上面的修復,因為解雇動畫有點難看:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.