2008-10-21 52 views
5

我想隱藏的UISearchBar大部分時間,只把它當用戶希望它出現。滑動的UISearchBar

我已經把一個UISearchBar放在界面生成器中,並將它隱藏在一個視圖後面,當用戶點擊一個按鈕時,它會調用下面的代碼,我希望它會將搜索欄放到前面並滑動鍵盤來查看。但它不....

- (IBAction)search:(id)sender 
{ 
    [mySearchBar performSelector:@selector(searchBarTextDidBeginEditing:)]; 
} 

任何人都知道如何通過代碼激活UISearchBar?

謝謝。

感謝您的回覆。我只是發佈完整的代碼,以防某人感興趣

- (IBAction)search:(id)sender 
{ 
    mySearchBar.hidden = NO; 
    [mySearchBar becomeFirstResponder]; 
} 

#pragma mark UISearchBarDelegate delegate methods 

// called when keyboard search button pressed 
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar 
{ 
    mySearchBar.hidden = YES; 
    [mySearchBar resignFirstResponder]; 
// Do something with the mySearchBar.text 
} 

// called when cancel button pressed 
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar 
{ 
    mySearchBar.hidden = YES; 
    [mySearchBar resignFirstResponder]; 
} 

回答

4

如果你想彈出鍵盤,你需要調用[mySearchBar becomeFirstResponder]

+0

這是很棒的提示,歡呼聲 – Dan 2008-10-21 12:49:56

2

我將搜索欄放在視圖頂部並隱藏起來。那麼你只需要:

mySearchBar.hidden = NO; 

來顯示它。根據你在做什麼,你可能還需要調整底層視圖的大小(否則它的頂部將被裁剪)。它仍然不會「滑入」視野。使用適當地附加搜索欄的全新視圖可能更容易。