標籤:
一、隱藏自身軟鍵盤
當對於有多個UITextField控制項都想通過點擊“Return”來隱藏自身軟鍵盤的情況。這時的最好辦法是使用Did End on Exit事件。在點擊軟鍵盤右下角的“Return”button後,會觸發該事件。
該事件有一個sender參數表示當前文字框。這樣便能夠編寫一個通用的事件處理方法(.m檔案)——
- (IBAction)TextField_DidEndOnExit:(id)sender { // 隱藏鍵盤. [sender resignFirstResponder]; }
然後在.h檔案裡填寫該方法的聲明——
- (IBAction)TextField_DidEndOnExit:(id)sender;
回到storyboard。並按command+option+enter開啟輔助表單,使輔助表單顯示.h檔案。
選中一個UITextField控制項,點擊滑鼠右鍵彈出面板,滑鼠左鍵按住Did End on Exit事件旁邊的圓圈,然後拖曳到右側.h檔案的TextField_DidEndOnExit方法上,便會建立好事件串連。 隨後依照相同的做法。將其它UITextField控制項的Did End on Exit事件也串連到TextField_DidEndOnExit方法。
運行一下,可發現每一個文字框的軟鍵盤都能夠通過點擊“Return”來隱藏了。
二、點擊Return自己主動轉到下個文字框
當頁面中有非常多個文字框時,假設每次都須要點文字框啟用軟鍵盤、輸入後點擊Return隱藏軟鍵盤、再點擊下一個文字框……這樣操作起來太繁瑣了。 於是我們希望能夠實現點擊Return時能夠自己主動轉到下一個文字框。
尤其是對於最後一個文字框,希望能夠在點擊Return時運行下一步操作。
比如對於登入頁面。它上面有 帳號文字框(nameTextField)、password文字框(passTextField)、登入button(loginButton)。
我們希望——點擊帳號文字框軟鍵盤的Return時跳轉到password文字框,點擊password文字框軟鍵盤的Return時運行登入。
由於這兩個文字框的功能不同,不能像上一節那樣寫一個TextField_DidEndOnExit做統一處理。而應該分別建立各自的事件處理方法。
回到storyboard,右擊帳號文字框(nameTextField)彈出面板,按住Did End on Exit事件旁邊的圓圈。然後拖曳到右側.h檔案的空白地方,此時會彈出一個對話方塊給方法命名。輸入名稱 (nameTextField_DidEndOnExit)後斷行符號確定,便自己主動產生了該事件方法。
隨後依照相同的做法,為password文字框(passTextField)的Did End on Exit事件串連方法(passTextField_DidEndOnExit)。
來到.m檔案。填寫詳細代碼——
- (IBAction)nameTextField_DidEndOnExit:(id)sender { // 將焦點移至下一個文字框. [self.passTextField becomeFirstResponder]; } - (IBAction)passTextField_DidEndOnExit:(id)sender { // 隱藏鍵盤. [sender resignFirstResponder]; // 觸發登陸button的點擊事件. [self.loginButton sendActionsForControlEvents:UIControlEventTouchUpInside]; }
對於帳號文字框轉password文字框。不須要隱藏軟鍵盤。僅僅須要調用becomeFirstResponder啟用新的文字框即可了。
對於password文字框Return後運行登入。
由於不再須要顯示軟鍵盤,所以還是得調用resignFirstResponder隱藏軟鍵盤,然後觸發登入button(loginButton)的UIControlEventTouchUpInside事件進行登入。
運行一下,可發現已經達到我們希望的效果了。
點擊帳號文字框軟鍵盤的Return時跳轉到password文字框。點擊password文字框軟鍵盤的Return時運行登入。
怎麼都是“Return”,轉換文字框與運行登入明明是不同的功能?
於是將帳號文字框的Return Key屬性設為“Next”,將password文字框的Return Key屬性設為“Done”。使介面與功能一致。
三、輕觸背景隱藏軟鍵盤
僅僅能通過Return關閉軟鍵盤太不靈活了。應該提供輕觸背景隱藏軟鍵盤的功能。
在storyboard,點擊背景View,將它的Custom Class設定為UIControl。這樣才會出現Touch Down事件。
右擊背景View彈出面板,按住Touch Down事件旁邊的圓圈,然後拖曳到右側.h檔案的空白地方建立該事件的處理方法。
來到.m檔案,填寫詳細代碼——
- (IBAction)View_TouchDown:(id)sender { // 發送resignFirstResponder. [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil]; }
四、自己定義鍵盤
原始碼能夠到我的github中下載:https://github.com/colin1994/myKeyboard.git
能夠通過自己定義鍵盤, 在鍵盤上增加你須要的功能, 即可。
效果例如以下:
代碼例如以下:
- (void)viewDidLoad{ [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib. if (self.keyboardToolbar == nil) { self.keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 38.0f)]; self.keyboardToolbar.barStyle = UIBarStyleBlackTranslucent; UIBarButtonItem *previousBarItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"前進", @"") style:UIBarButtonItemStyleBordered target:self action:@selector(previousField:)]; UIBarButtonItem *nextBarItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"後退", @"") style:UIBarButtonItemStyleBordered target:self action:@selector(nextField:)]; UIBarButtonItem *spaceBarItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; UIBarButtonItem *doneBarItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"隱藏", @"") style:UIBarButtonItemStyleDone target:self action:@selector(resignKeyboard:)]; [self.keyboardToolbar setItems:[NSArray arrayWithObjects:previousBarItem, nextBarItem, spaceBarItem, doneBarItem, nil]]; } self.myTextView.inputAccessoryView = self.keyboardToolbar;}#pragma mark - your code- (void)resignKeyboard:(id)sender{ [self.myTextView resignFirstResponder];}- (void)previousField:(id)sender{}- (void)nextField:(id)sender{}
iOS開發- 隱藏鍵盤總結