2011-12-26 205 views
0

當我添加一個uisearchbar並點擊它時,我得到一個鍵盤和一個Search按鈕。我需要訪問這個按鈕,例如當用戶在search field中輸入一些字符串並點擊搜索時,我需要用他輸入的文本打印一個NSLog編程鍵盤上的搜索按鈕

1.)如何訪問此搜索按鈕的IBAction

2.)我還需要在用戶點擊背景時關閉鍵盤,我怎樣才能以編程方式執行這些操作?

回答

0

有你實現搜索欄的委託方法:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar 
{ 
    NSLog(@"Search string",[searchBar text]); 
} 
0

實現UISearchBarDelegate

1) 實現該方法

//這相當於IBAction爲你需要的

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar 
{ 

} 

2) 有搜索欄的出口,說其名,_searchBar。 執行touchesEnded方法如下。

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [_searchBar resignFirstResponder]; 
} 

這是隱藏鍵盤。

2

使用的UISearchBar委託,你可以「檢測」既:在搜索欄搜索按鈕或鍵盤上的搜索按鈕:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{ 
    [self buttonSearchPressed]; 
} 

-(IBAction)buttonSearchPressed{ 
    .... 
    [yoursearchbar resignFirstResponder]; 
}