A variety of ways to put your virtual keyboard away-IOS

Source: Internet
Author: User
Tags uicontrol

Using a virtual keyboard to enter information is one of the most important ways iOS can interact, and a virtual keyboard will usually automatically appear in an editable Uitextfield or Uitextview editing event, but it's easy to call out a keyboard, but to put it away, it's not as simple as before Uitextfield how to close the keypad after entering the end of the article, describes how to close the virtual keyboard after editing, but if your component does not have a corresponding event to let you put away the virtual keyboard, then what to do? The following is an example of a similar component Uitextview, which describes some common methods.

1. Click outside the edit area (UIView)

This is a very intuitive way, when you no longer need to use the virtual keyboard, just click on the virtual keyboard and the editing area outside, you can put the keyboard, the following code is built in the UIView Touch event method function, you can refer to touch Panel/touch screen/ Basic use of pressure sensors in this article, find out more about the method functions for touch events.

C code   
    1. -(void) touchesended: (Nsset *) touches withevent: (Uievent *) event {
    2. if (![ Mytextview Isexclusivetouch]) {
    3. [Mytextview Resignfirstresponder];
    4. }
    5. }

If you want to use this method, be sure to remember that the Custom Class of your operation screen must be UIView.

The Custom Class of the screen is UIView

2. Click outside the editing area (Uicontrol)

You can put the virtual keyboard in the same way as the previous one, but if your touch event is already full of code, consider using the Uicontrol touch up Inside event to pick up the keyboard by writing the following code with Uicontrol touch up Inside Event link.

C code  
    1. -(Ibaction) Dismisskeyboard: (ID) Sender {
    2. [Mytextview Resignfirstresponder];
    3. }

If you want to use this method, be sure to remember that the Custom Class of your operation screen must be uicontrol.

The Custom Class of the screen is Uicontrol

Link the method of the keyboard to the Uicontrol event

3. Use the button that makes the keyboard to be closed

It is also a good idea to use a button to put away the current virtual keyboard when there is no editing area outside of the editor, so it is important to borrow the nsnotificationcenter if the button must appear in the virtual keyboard to be displayed on the screen. To help us determine the current state of the keyboard, you can find more information about Nsnotificationcenter in the Nsnotificationcenter article like Observer Pattern.

First in the Viewdidload: event, register to Nsnotificationcenter, tell Nsnotificationcenter our Donebuttonshow: Method function, want to subscribe to the Uikeyboarddidshownotification message events.

C code   
    1. -(void) Viewdidload {
    2. [Super Viewdidload];
    3. [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (donebuttonshow:) Name: Uikeyboarddidshownotification Object:nil];
    4. }

Now whenever the virtual keyboard appears, it will automatically call our custom Donebuttonshow: Method function, and then simply define the method that the button appears in the method function.

C code
    1. -(void) Donebuttonshow: (nsnotification *) Notification {
    2. Donebutton = [UIButton buttonwithtype:uibuttontyperoundedrect];
    3. Donebutton.frame = CGRectMake (0, 228, 70, 35);
    4. [Donebutton settitle:@"Finish editing" forstate:uicontrolstatenormal];
    5. [Donebutton addtarget:self Action: @selector (Hidekeyboard) forcontrolevents:uicontroleventtouchupinside];
    6. [Self.view Addsubview:donebutton];
    7. }

Finally, the Hidekeyboard when the push button is pressed: Method function, be sure to remember to remove the button in the function.

C code   
    1. -(void) Hidekeyboard {
    2. [Donebutton Removefromsuperview];
    3. [Mytextview Resignfirstresponder];
    4. }

4. Input characters using judgment

If you want to use input-specific characters (for example, return newline characters) to close the keyboard, you must first adopt a contract within the @interface section of the category, and you can refer to the use of the Protocol contract article for more information about the agreement.

After the agreement is adopted, the TextView:shouldChangeTextInRange:replacementText in the agreement is then made: The method function, which is triggered when the character input is entered, and the BOOL value of the postback indicates whether the character is to function, The following code is in this method function, the use of the way to determine the input character to the virtual keyboard (to determine the character is return newline characters).

C code < param name= "Quality" value= "High" >  
    1. -(BOOL) TextView: (Uitextview *) TextView Shouldchangetextinrange: (nsrange) Range Replacementtext: (NSString *) text {  
    2. if ([text isequaltostring:@"\ n"]) {
    3. [Mytextview Resignfirstresponder];
    4. return NO;
    5. }
    6. return YES;
    7. }

Finally, don't forget to point the proxy object of Uitextview to yourself in the Viewdidload: event so that the program can correctly find the class of the actual contract method function.

C code  
    1. -(void) Viewdidload
    2. {
    3. [Super Viewdidload];
    4. Mytextview.delegate = self;
    5. }

5. Questions about keyboard masking

If you have encountered the problem of the keyboard masking editing area in the implementation, you can refer to using Animation to solve the problem of the keypad block Uitextfield, through the Core Graphic Animation function, Move the editing area while the keyboard appears to resolve the masking problem.

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.