iOS Development-Hide Keyboard summary

Source: Internet
Author: User

First, hide their own soft keyboard
When there are multiple Uitextfield controls that want to hide their soft keyboard by clicking "Return". The best way to do this is to use the Do End on Exit event. This event is triggered when you click the "Return" button in the lower-right corner of the soft keyboard.
The event has a sender parameter that represents the current text box. This allows you to write a common event handling method (. m file)--


Then fill in the. h file with the declaration of the Method--


Back to Storyboard. and press Command+option+enter to open the secondary form so that the secondary form displays the. h file.

Select a Uitextfield control, click the right mouse button pop-up panel, the left mouse button hold the circle next to the Do End on Exit event, and then drag to the right side. h file on the Textfield_didendonexit method, the event connection is established. Then follow the same procedure. The Do End on Exit event for other Uitextfield controls is also connected to the Textfield_didendonexit method.

Running, you can find the soft keyboard of each text box can be hidden by clicking "Return".

Second, click Return to take the initiative to go to the next text box
When there are more than one text box in the page, assume that each time you need a text box to activate the soft keyboard, enter and click Return to hide the soft keyboard, and then click the next text box ... This is too cumbersome to operate. So we want to be able to move to the next text box on our own when we click Return.

Especially for the last text box, you want to be able to run the next action when you click Return.

For example, the login page. It has an account text box (Nametextfield), a Password text box (Passtextfield), and a login button (Loginbutton).


We want to---click the return of the Account text box on the soft keyboard to jump to the Password text box and click the return of the Password text box soft keyboard to run the login.


Because the functions of these two text boxes are different, it is not possible to write a textfield_didendonexit as in the previous section to do unified processing. Instead, they should establish their own event-handling methods respectively.

Go back to Storyboard, right-click the Account text box (Nametextfield) flyout panel, and hold down the circle next to the Do End on Exit event. Then drag to a blank place on the right side of the. h file, and a dialog box will pop up to give the method a name. After entering the name (nametextfield_didendonexit), enter OK and the event method is generated on its own initiative.
Then follow the same procedure for the Do End on Exit Event connection method (Passtextfield_didendonexit) for the Password text box (Passtextfield).
Came to the. m file. Fill in the detailed code--



For the Account text box, go to the Password text box. There is no need to hide the soft keyboard. Just call Becomefirstresponder to activate the new text box.
Run the login for the password text box after return.

Since it is no longer necessary to display the soft keyboard, you still have to call Resignfirstresponder to hide the soft keyboard, and then trigger the login button (Loginbutton) UIControlEventTouchUpInside event to log in.



Run, and you can see that we have achieved the desired effect.

Click Return on the soft keyboard of the Account text box to jump to the password text box. Click the return of the Password text box soft keyboard to run the login.
How is "Return", convert text box and run login obviously is different function?
The return key property of the Account text box is set to "Next" and the return key property of the Password text box is set to "done". Make the interface and function consistent.

Third, touch the background to hide the soft keyboard
It is too inflexible to simply turn off the soft keyboard by return. You should provide the ability to touch the background to hide the soft keyboard.

In storyboard, click Background view to set its custom class to Uicontrol. This will cause the touch down event to occur.
Right-click the background View pop-up panel, press and hold the circle next to the touch down event, and then drag to the right side. h file to create a way to handle the event.
Come to the. m file, fill in the detailed code--



Iv. Define your own keyboard

Source code can be downloaded to my github: https://github.com/colin1994/myKeyboard.git

You can define the keyboard by yourself and add the functions you need on the keyboard.

Effects such as the following:



The code is as follows:

-(void) viewdidload{[Super viewdidload];//do no 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 (@ "forward" @ "")                                                                           Style:uibarbuttonitemstylebordered                                                                           Target:self                Action: @selector (Previousfield:)];                                                                        Uibarbuttonitem *nextbaritem = [[Uibarbuttonitem alloc] initwithtitle:nslocalizedstring (@ "Back" @ "") Style:uibarbuttonitemstylebordered                                                                      Target:self                Action: @selector (NextField:)]; Uibarbuttonitem *spacebaritem = [[Uibarbuttonitem alloc] Initwithbarbuttonsystemitem: Uibarbuttonsystemitemflexiblespace TA                Rget:nil Action:nil];                                                                        Uibarbuttonitem *donebaritem = [[Uibarbuttonitem alloc] initwithtitle:nslocalizedstring (@ "hidden" @ "")                                                                       Style:uibarbuttonitemstyledone Target:self Action: @sel                Ector (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 Development-Hide Keyboard summary

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.