簡體   English   中英

UISearchBar委托沒有響應取消按鈕

[英]UISearchBar delegate not responding to cancel button

我有一個UIViewController ,它是一個UISearchBarDelegate和一個MKMapViewDelegate searchBarSearchButtonClicked事件運行正常,但在iOS 4.2中進行測試時,在點擊取消按鈕時,不會調用searchBarCancelButtonClicked。 在4.3中一切正常。 我有相同代碼的其他視圖,它工作正常。 我已經三次檢查了方法簽名。

它可能與MapView有關,還是我做了一些明顯的錯誤?

我的.h文件:

@interface MyViewController : UIViewController <UISearchBarDelegate,MKMapViewDelegate,UIAlertViewDelegate>{
MKMapView *mapMainView;
UISearchBar *sBar;

}

@property (nonatomic, retain) UISearchBar *sBar;
@end

我創建搜索欄如下:

sBar = [[[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320.0, 70.0)] autorelease];
sBar.delegate = self;
sBar.showsCancelButton = YES;
[self.view addSubview:sBar];
[sBar becomeFirstResponder];

方法:

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
searchBar.hidden = YES;
}

有沒有人知道為什么會這樣?

我有同樣的問題。 按住取消按鈕幾秒鍾。

我的原因是我在tableview中實現了UITapGestureRecognizer。 因此,這優先於按鈕單擊或搜索欄中的“x”按鈕單擊。

在我的情況下,解決方案是將手勢識別限制為僅僅查看tableview的backgroundview。 我猜你的情況可能會發生類似的事情。 嘗試將手勢識別器限制為所需的最小子視圖,搜索欄應位於該視圖之外。

可能你的sbar對象正在釋放,在這種情況下是一個自動釋放對象,為什么? 嘗試將sBar聲明為IBOutlet屬性。 在Interface Builder中創建apropiate鏈接,在編碼時刪除alloc,放入viewDidUnload self.sbar = nil; 並將其發布為dealloc。 在viewDidLoad中放這個。

sBar.delegate = self;
sBar.showsCancelButton = YES; // this is an option in object inspector
[self.view addSubview:sBar];
[sBar becomeFirstResponder]; //remove this.

告訴我它是否有效

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        [self.searchDisplayController setActive:NO animated:YES];
        [self.searchDisplayController.searchBar resignFirstResponder];
    }
}

試試這個:

sBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320.0, 70.0)];

sBar.delegate = self;

sBar.showsCancelButton = YES;

[self.view addSubview:sBar];

並嘗試將釋放放入dealloc

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM