IOS Development --- disable the keyboard

Source: Internet
Author: User
Tags exit in

In iOS, if you want to input data in the text box, touch the text box to open the keyboard. For iPad apps, there is a button on the keyboard that can be used to close the keyboard, but the keyboard in the iPhone program does not have such a button, but we can take some ways to close it. For example, you can press the rerun (sometimes done or research) Key to close the keyboard, or, more humane, tap the background to close the keyboard.

1. Press return to close the keyboard.

When you press the return key on the keyboard, a did end on exit event is generated. In this case, we tell the text box to discard the control, and the keyboard disappears.

Assume that we have created a single view application and enabled viewcontroller. in the XIB file, three text fields are dragged on the view. Then, we map the three text fields to viewcontroller. in H, the names are firstfield, secondfield, and thirdfield. For example:

On this basis, you can tap return to close the keyboard. The steps are as follows:

(1) declare a method in viewcontroller. h:

- (IBAction)textFiledReturnEditing:(id)sender;

(2) implement this method in viewcontroller. M:

-(IBAction)textFiledReturnEditing:(id)sender {    [sender resignFirstResponder];}

The so-called first responder refers to the control with which the user is currently interacting. When a user uses a keyboard, the first responder is the keyboard. The resignfirstresponder method, as the name suggests, is to discard the first responder.

(3) map all three text boxes to the textfiledreturnediting method. However, the event should be did end on exit. The specific operation is as follows:

Open the assistant editor, and open viewcontroller on the left. XIB. Open viewcontroller on the right. h. Open connector inspector on the rightmost side of xcode, select the first text box in view, find did end on exit in connector inspector, and pull the ing line from the circle on the Right of xcode, maps to viewcontroller. the textfiledreturnediting method of H, for example:

Perform the same operation on the other two text boxes. Now, you can touch the return key to close the keyboard.

2. Next we will introduce a more user-friendly method. Tap the background to close the keyboard.

Similar to the above steps, first define a method, then implement this method, then map the specified control to this method, and select the triggered event. The difference is that the control we want to select this time is not the text box above, but the view itself.

(1) Add the method declaration code to the viewcontroller. h file:

- (IBAction)backgroundTap:(id)sender;

(2) implement this method in viewcontroller. M:

- (IBAction)backgroundTap:(id)sender {    [firstField resignFirstResponder];    [secondField resignFirstResponder];    [thirdField resignFirstResponder];}

It should be noted that [firstfield resignfirstresponder]; indicates that if firstfield has firstresponder, discard it. We do not need to judge whether firstfield exists first. This statement is completely correct.

(3) map the view to this method. However, we need to change the view type first.

Open XIB, select view, enable identity inspector, and select uicontrol in class:

(4) Open the assistant editor, and open viewcontroller on the left. XIB. Open viewcontroller on the right. h. Open connector inspector at the far right of xcode and go to viewcontroller. select Control in XIB, find touch down in connector inspector, and pull the ing line from the circle on the right to map it to viewcontroller. h's backgroundtap method, such:

Okay, you can run the following command to check the effect:

After opening the keyboard, click in the background area and the keyboard will be folded down.

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.