當我添加一個uisearchbar
並點擊它時,我得到一個鍵盤和一個Search
按鈕。我需要訪問這個按鈕,例如當用戶在search field
中輸入一些字符串並點擊搜索時,我需要用他輸入的文本打印一個NSLog
。編程鍵盤上的搜索按鈕
1.)如何訪問此搜索按鈕的IBAction
?
2.)我還需要在用戶點擊背景時關閉鍵盤,我怎樣才能以編程方式執行這些操作?
當我添加一個uisearchbar
並點擊它時,我得到一個鍵盤和一個Search
按鈕。我需要訪問這個按鈕,例如當用戶在search field
中輸入一些字符串並點擊搜索時,我需要用他輸入的文本打印一個NSLog
。編程鍵盤上的搜索按鈕
1.)如何訪問此搜索按鈕的IBAction
?
2.)我還需要在用戶點擊背景時關閉鍵盤,我怎樣才能以編程方式執行這些操作?
有你實現搜索欄的委託方法:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
NSLog(@"Search string",[searchBar text]);
}
1) 實現該方法
//這相當於IBAction爲你需要的
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
}
2) 有搜索欄的出口,說其名,_searchBar。 執行touchesEnded方法如下。
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[_searchBar resignFirstResponder];
}
這是隱藏鍵盤。
使用的UISearchBar委託,你可以「檢測」既:在搜索欄搜索按鈕或鍵盤上的搜索按鈕:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
[self buttonSearchPressed];
}
-(IBAction)buttonSearchPressed{
....
[yoursearchbar resignFirstResponder];
}