IOS: 4 ways to make your keyboard disappear

Source: Internet
Author: User

Transferred from: http://leopard168.blog.163.com/blog/static/168471844201422121310352/

In the iOS app, as long as you use the edit box (Uitextfield), you have to deal with the keyboard disappearing (dismiss keyboard). I have used 4 different methods to deal with the problem of keyboard disappearance. 1. Release the first responder (Resignfirstresponder) through Uitextfileddelegate, the code for releasing the keyboard via delegate is as follows:

-(BOOL) Textfieldshouldreturn:( Uitextfield *) TextField  {

[TextField resignfirstresponder];

returnYES;

}

The following 3 ways are handled by responding to events:

2. Through the Didonexit method of the Done key, to Resignfirstresponder;

3. Click the background image of the edit box to make the keyboard disappear. Method: Set the background view to: Uicontroll. The UIView itself cannot respond to events, while Uicontroll can respond to events. Uicontroll inherits from UIView; makes the keyboard disappear by invoking the Touch-down event of Uicontroll;

4. Place a UIButton to make the UIButton full screen, and when you click on a non-editable area, click on the Giant button. In this button event, the Resignfirstresponder can be.

- (ibaction)backgroundtap:(ID) Sender {

[self. Nametextfield Resignfirstresponder ];

[self. Passwordtextfield Resignfirstresponder ];

}

Description: this Method simulates two behaviors of a user:  

1. Click on the Done button on the keyboard to make the keyboard disappear;

2. Click outside the edit box to make the keyboard disappear.

Special Note: through full-screen Button , this method requires: this giant. Button be sure to put it at the bottom. Otherwise, the edit box will not accept the input events.

Program Reviews:

uitextfielddelegate and the Uicontroll program to deal with the keyboard disappearing problem, which is a great wisdom. and the giant Button and didonexit method, although can also use, but from the software programming thinking, can only be considered a kind of smart.

Summary:

whenever an input box exists, you have to deal with the problem of how to make the keyboard disappear. Of course, you can just handle clicking on the "done" button to make the keyboard disappear. However, in order for the user to have a better experience, you need to handle the event response of a zone that is not an edit box.

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.