iOS developing hidden keyboard methods Summary

Source: Internet
Author: User

iOS development keyboard is a frequent need to deal with the place, below for everyone to bring me to summarize several methods of hiding the keyboard.

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, so you can write a common event-handling method (. m file).

    1. -(Ibaction) Textfield_didendonexit: (ID) Sender {
    2. //Hide keyboard.
    3. [Sender Resignfirstresponder];
    4. }

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

    1. -(Ibaction) Textfield_didendonexit: (ID) sender;

Go back to Storyboard and press Command+option+enter to open the secondary window so that the secondary window 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. h file on the Textfield_didendonexit method, the event connection is established. You then follow the same procedure to connect the Do End on exit events of other Uitextfield controls to the Textfield_didendonexit method.

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

Second, click Return to automatically go to the next text box

When there are many text boxes in the page, if you need a text box each time to activate the soft keyboard, enter and click Return to hide the soft keyboard, then click the next text box ... This is too cumbersome to operate. So we want to be able to automatically go to the next text box when we click Return. Especially for the last text box, you want to be able to do the next step 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 hope--click on the return of the Account text box soft keyboard to jump to the Password text box, click on the Password text box soft keyboard return when you perform a login.

Because the functions of the two text boxes are different, it is not possible to write a textfield_didendonexit as in the previous section, but to set up separate event handling methods.

Go back to Storyboard, right-click the Account text box (Nametextfield) flyout panel, hold down the circle next to the Do End on Exit event, and drag to the right. 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 automatically generated.

Then follow the same procedure for the Do End on Exit Event connection method (Passtextfield_didendonexit) for the Password text box (Passtextfield).

Come to the. m file, fill in the specific code--

    1. -(Ibaction) Nametextfield_didendonexit: (ID) Sender {
    2. //Move focus to the next text box.
    3. [Self.passtextfield Becomefirstresponder];
    4. }
    5. -(Ibaction) Passtextfield_didendonexit: (ID) Sender {
    6. //Hide keyboard.
    7. [Sender Resignfirstresponder];
    8. //trigger a Click event for the login button.
    9. [Self.loginbutton Sendactionsforcontrolevents:uicontroleventtouchupinside];
    10. }

For the Account text box to the Password text box, do not need to hide the soft keyboard, just call Becomefirstresponder to activate the new text box on the line.

Perform a login for the password text box after return. Because the soft keyboard is no longer needed, it is still necessary 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 on the return of the soft keyboard of the Account text box to jump to the Password text box, click on the Password text box soft keyboard return when you perform a login.

How is "Return", convert text box and execute 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" to make the interface consistent with the function.

Third, touch the background to hide the soft keyboard

The soft keyboard can only be turned off by return too inflexible and 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 so that the touch down event appears.

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. h file to create a way to handle the event.

Come to the. m file, fill in the specific code--

    1. -(Ibaction) View_touchdown: (ID) Sender {
    2. //Send Resignfirstresponder.
    3. [[UIApplication sharedapplication] sendaction: @selector (resignfirstresponder) To:nil From:nil Forevent:nil];
    4. }

(Via:zz_mm's Column)

iOS developing hidden keyboard methods Summary

Related Article

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.