2012-03-26 54 views
-1

簡單的問題:如果我的「完成」鍵是Search,我怎麼能夠辭掉我的文本字段。從本質上講,做我做,如果用戶不希望搜索,而是要取消......resignFirstResponder for UITextField with search returnkey

感謝

+0

在導航欄中放置一個條形按鈕,通過退出第一個響應者來隱藏鍵盤。 – 2012-03-26 04:47:28

+0

同樣的問題是這樣的:-http://stackoverflow.com/questions/8558339/how-i-dismiss-keyboard-on-tapping-search-button-on-keyboard-regarding-uisearchba – Leena 2012-03-26 04:48:04

回答

2

您可以使用:爲搜索欄取消按鈕和需要實現這個SearchBar delegate

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar 
{ 
    isSearching = NO; //This is a flag which specifies if searching is going on or not. 
    searchBar.text = @""; //Clears out search bar text 
    [self resetSearch]; //Reset search resets all the flags and variables. 
    [self.leadsTable reloadData]; //Reloads the tableView 
    [searchBar resignFirstResponder]; //Resigns responder from Search bar 
} 

這是辭職的響應,如果用戶不希望搜索有道。

看看如何在UISearchBar中添加一個內置的取消按鈕。檢查屬性 「演出取消按鈕」(紅色箭頭高亮)

enter image description here

編輯:

STEP-1: 檢查有條件的文本框的文本是否爲空?如果是這樣的resignFirstResponder爲TextField。您需要委派設置爲self給出UITextField使用代碼:

txtField.delegate = self; 

或者您也可以通過文本域的delegate連接到File's Owner做到從界面生成器一樣。

第2步:您需要實現此委託方法。

- (BOOL)textFieldShouldReturn:(UITextField *)textField { 
    if([textField.text isEqualToString:@""]) 
    { 
     [textField resignFirstResponder]; 
    } 
    return YES; 
} 

編輯-1:

現在創建一個標有 '取消'

一個欄按鈕UIToolbar一旦你做到這一點:

您可以編寫一個按鈕單擊事件對於UIToolBar:

-(IBAction)cancelClicked:(id)sender 
{ 
    [txtField resignFirstResponder]; 
} 

O你已經完成,你現在可以寫:

txtField.inputAccessoryView = toolbarOutlet; 

希望這可以幫助你。

+0

這不是我所尋找的,因爲textField位於UITableView單元格內部,而不是UISearchBar。儘管謝謝你的回答。 – Apollo 2012-03-26 15:47:59

+0

@ derek.lo:在UITableViewCell下有UITextField而不是UISearch欄的具體原因是什麼? – 2012-03-26 15:49:13

+0

textfield並不是要在tableview中搜索結果,而是爲了在DB中發送一個項目的請求。因此它在單元格中看起來更好。 – Apollo 2012-03-26 15:51:09

1

在返回按鈕上的文本是不相關的討論。你想知道如何在不按下回車按鈕的情況下退出第一響應者,並且有幾種方法可以實現。

  1. 使用文本字段的inputAccessoryView在鍵盤上方的工具欄中顯示單獨的取消按鈕。
  2. 在字段的超級視圖上使用輕擊手勢識別器可識別用戶何時輕擊場外,並致電[self.view endEditing:YES](其中self是您的視圖控制器)。這將導致第一響應者辭職。 (這在滾動視圖中非常挑剔。)
  3. 在編輯時,假設當前屏幕上有UINavigationBar,在編輯時將當前視圖控制器的rightBarButtonItem替換爲取消欄按鈕項目。編輯結束後,如果有的話,換回常規的右欄按鈕項目。
+0

是的,這是我需要的知道。謝謝@warrenm – Apollo 2012-03-26 15:48:14