IOS development-hide keyboard Summary

Source: Internet
Author: User

1. Hide your keyboard
When multiple UITextField controls want to hide their own keypad by clicking "Return", the best way is to use the Did End on Exit event. This event is triggered when you click "Return" in the lower-right corner of the keyboard.
This event has a sender parameter indicating the current text box, so that you can write a common event processing method (. m file )--
-(IBAction) TextField_DidEndOnExit :( id) sender {// hide the keyboard. [sender resignFirstResponder];}


Then fill in the declaration of this method in the. h file --

- (IBAction)TextField_DidEndOnExit:(id)sender; 

Return to the storyboard, and press command + option + enter to open the secondary window, so that the. h file is displayed in the secondary window. Select a UITextField control, right-click the panel, and press and hold the circle next to the Did End on Exit event with the left mouse button, and drag it to the right. the event connection is established on the TextField_DidEndOnExit method of the H file. Then, in the same way, connect the Did End on Exit event of other UITextField controls to the TextField_DidEndOnExit method.

Click "Return" to hide the keyboard of each text box.

2. Click Return to automatically go to the next text box
When there are many text boxes on the page, if you need to click the text box to activate the soft keyboard every time, enter the soft keyboard, click Return to hide the soft keyboard, and then click the next text box ...... This operation is too cumbersome. So we hope to automatically go to the next text box when we click Return. Especially for the last text box, you want to perform the next operation when you click Return.

For example, for the logon page. It contains the account text box (nameTextField), passTextField, and loginButton ).
We hope to jump to the Password text box when you click Return in the account text box, and click Return in the Password text box to execute logon.
Because the functions of the two text boxes are different, you cannot write a TextField_DidEndOnExit as in the previous section for unified processing. Instead, you should create respective event processing methods.

Return to the storyboard, right-click the account text box (nameTextField) to bring up the Panel, hold down the circle next to the Did End on Exit event, and drag it to the right. A dialog box is displayed to name the method. Enter the name (nameTextField_DidEndOnExit) and press enter to confirm. The event method is automatically generated.
In the same way, the connection method of the Did End on Exit event (passTextField_DidEndOnExit) in the passTextField is used ).
Go to the. m file and enter the specific code --

-(IBAction) nameTextField_DidEndOnExit :( id) sender {// move the focus to the next text box. [self. passTextField becomeFirstResponder];}-(IBAction) passTextField_DidEndOnExit :( id) sender {// hide the keyboard. [sender resignFirstResponder]; // click the event to trigger the login button. [self. loginButton sendActionsForControlEvents: UIControlEventTouchUpInside];}


For the account text box to the Password text box, you do not need to hide the keyboard, you just need to call becomeFirstResponder to activate the new text box.
Return the Password text box and execute logon. Because the keyboard is no longer needed, you must call resignFirstResponder to hide the keyboard, and then trigger the UIControlEventTouchUpInside event of the loginButton to log on.

Run it and we can see that the expected results have been achieved. Click Return in the account text box to jump to the Password text box. Click Return in the Password text box to log on.
Why is it "Return"? The conversion text box is different from the login execution function?
Set the Return Key attribute of the account text box to "Next", and set the Return Key attribute of the Password text box to "Done", so that the interface is consistent with the function.

3. Touch the background to hide the keyboard
It is too inflexible to close the keypad only by using Return. You should provide the function of tapping the background to hide the keypad.

On the storyboard, click the background View and set its Custom Class to UIControl to display the Touch Down event.
Right-click the background View pop-up panel, hold Down the circle next to the Touch Down event, and drag it to the blank area of the Right. h file to create a method for handling the event.
Go to the. m file and enter the specific code --

-(IBAction) View_TouchDown :( id) sender {// send resignFirstResponder. [[UIApplication sharedApplication] sendAction: @ selector (resignFirstResponder) to: nil from: nil forEvent: nil];}


4. Custom keyboard

The source code can be downloaded in my github: https://github.com/colin1994/myKeyboard.git

You can use a custom keyboard to add the required functions to the keyboard.

The effect is as follows:



The Code is as follows:

-(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, 38366f)]; self. keyboardToolbar. barStyle = UIBarStyleBlackTranslucent; UIBarButtonItem * previusbaritem = [[UIBarButtonItem alloc] initWithTitle: NSLocalizedString (@ "", @ "") style: Custom target: self action: @ selector (previusfield :)]; UIBarButtonItem * nextBarItem = [[UIBarButtonItem alloc] initWithTitle: NSLocalizedString (@ "", @ "") style: Invalid target: self action: @ selector (nextField :)]; UIBarButtonItem * spaceBarItem = [[UIBarButtonItem alloc] Destination: target: nil action: nil]; optional * doneBarItem = [[UIBarButtonItem alloc] initWithTitle: NSLocalizedString (@ "hide", @ "") 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) previusfield :( id) sender {}-(void) nextField :( id) sender {}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.