Ios-a variety of ways to close up a virtual keyboard
Source: Internet
Author: User
<span id="Label3"></p><p><p>This article was transferred from-http://justcoding.iteye.com/blog/1476171</p></p><p><p>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.</p></p><p><p>1. Click outside the edit area (UIView)</p></p><p><p>This is a straightforward 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 of the Touch event method Function.</p></p><p><p>C code</p></p><pre><pre><span style="color: #008080;">1</span> -(<span style="color: #0000ff;">void</span>) toucheended: (nsset *) touches withevent: (uievent *)<span style="color: #0000ff;">event</span><span style="color: #000000;">{</span><span style="color: #008080;">2</span> <span style="color: #0000ff;">if</span>(! <span style="color: #000000;"> [mytextview Isexclusivetouch]) {</span><span style="color: #008080;">3</span><span style="color: #000000;"> [mytextview resignfirstresponder]; </span> <span style="color: #008080;">4</span> <span style="color: #000000;"> } </span><span style="color: #008080;">5</span> }</pre></pre><p><p><br>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</p></p><p><p>2. Click the edit area (uicontrol)</p></p><p><p>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.</p></p><p><p>C code</p></p><pre><pre><span style="color: #008080;">1</span> -(<span style="color: #0000ff;">void</span>) dismisskeyboard: (<span style="color: #0000ff;">ID</span><span style="color: #000000;">) sender{</span><span style="color: #008080;">2</span><span style="color: #008080;">3</span><span style="color: #000000;"> </span><span style="color: #008080;">4 </span> <span style="color: #008080;">5</span> }</pre></pre><p><p>If you want to use this method, be sure to remember that you operate the screen of the custom Class must be uicontrol, will be the method of the keyboard and the Uicontrol event link</p></p><p><p>3. Use the button that only makes the keyboard</p></p><p><p>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.</p></p><p><p>First in the Viewdidload: event, register to nsnotificationcenter, tell Nsnotificationcenter our Donebuttonshow: method function, want to subscribe to the Uikeyboarddidshownotification message Events.</p></p><p><p>C code</p></p><pre><pre><span style="color: #008080;">1</span> -(<span style="color: #0000ff;">void</span><span style="color: #000000;">) viewdidload{</span><span style="color: #008080;">2</span><span style="color: #000000;"> [super viewdidload];</span> <span style="color: #008080;">3</span> <span style="color: #008080;">4</span> <span style="color: #0000ff;">Object</span> <span style="color: #000000;">: nil]; </span> <span style="color: #008080;">5</span> } </pre></pre><p><p>Now whenever the virtual keyboard appears, we automatically call our custom Donebuttonshow: method function, and then just define the method that the button appears in the new method Function.</p></p><p><p>C code</p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span>-(<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) Donebuttonshow: (nsnotification *<span style="color: #000000;"><span style="color: #000000;">) Notification</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #000000;"><span style="color: #000000;">{</span></span><span style="color: #008080;"><span style="color: #008080;">3</span></span>Donebutton =<span style="color: #000000;"><span style="color: #000000;">[UIButton buttonwithtype:uibuttontyperoundedrect];</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span>Donebutton.frame = CGRectMake (<span style="color: #800080;"><span style="color: #800080;">0</span></span>,<span style="color: #800080;"><span style="color: #800080;">228</span></span>,<span style="color: #800080;"><span style="color: #800080;"></span> -</span>,<span style="color: #800080;"><span style="color: #800080;"></span> *</span><span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span>[donebutton settitle:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Finish Editing</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">forstate:uicontrolstatenormal];</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span> <span style="color: #000000;"><span style="color: #000000;">[donebutton addtarget:self action: @selector (hidekeyboard) forcontrolevents:uicontroleventtouchupinside];</span></span><span style="color: #008080;"><span style="color: #008080;">7</span></span> <span style="color: #000000;"><span style="color: #000000;">[self.view addsubview:donebutton];</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span>}</pre><p><p>finally, the Hidekeyboard: method function when the button is pressed down, be sure to remember to remove the button from the Function.</p></p><p><p>C code</p></p><pre><pre><span style="color: #008080;">1</span> -(<span style="color: #0000ff;">void</span><span style="color: #000000;">) hidekeyboard</span><span style="color: #008080;">2</span><span style="color: #000000;">{</span><span style="color: #008080;">3</span><span style="color: #000000;"> [donebutton removefromsuperview];</span> <span style="color: #008080;">4</span> <span style="color: #000000;"> [mytextview resignfirstresponder];</span> <span style="color: #008080;">5</span> }</pre></pre><p><p>4. Input characters using the judgment segment</p></p><p><p>If you want to use input specific characters (such as return newline Characters) to close the keyboard, you must use the Contract @interface section within the category NOW.</p></p><p><p>After the adoption of the agreement, the TextView:shouldChangeTextInRange:replacementText in the agreement is then Made: method, which is triggered at the input of the character, and the bool value of the callback indicates whether the character is to function, The following code is in this method function, using the method of judging the input character to close the virtual keyboard (to determine the character is return newline characters).</p></p><p><p>C code</p></p><pre><pre>-(BOOL) textView: (uitextview *) textView shouldchangetextinrange: (nsrange) range replacementtext: (nsstring *<span style="color: #000000;">) text { </span><span style="color: #0000ff;">if</span> ([text isequaltostring:<span style="color: #800000;">@ "</span><span style="color: #800000;">\ n</span><span style="color: #800000;">"</span><span style="color: #000000;">]) { [mytextview resignfirstresponder]; </span> <span style="color: #0000ff;">return</span> <span style="color: #000000;">NO; } </span> <span style="color: #0000ff;">return</span> <span style="color: #000000;">YES;}</span></pre></pre><p><p>finally, don't forget to point the agent event of Uitextview to yourself in the Viewdidload: event so that the program can correctly find the category of the actual contract method Function.</p></p><p><p>C code</p></p><pre><pre><span style="color: #008080;">1</span> -(<span style="color: #0000ff;">void</span><span style="color: #000000;">) viewdidload</span><span style="color: #008080;">2</span><span style="color: #000000;">{</span><span style="color: #008080;">3</span><span style="color: #000000;"> [super viewdidload];</span> <span style="color: #008080;">4</span> Mytextview. <span style="color: #0000ff;">delegate</span> = self<span style="color: #000000;"> ;</span> <span style="color: #008080;">5</span> }</pre></pre><p><p></p></p><p><p>Ios-a variety of ways to close up a virtual keyboard</p></p></span>
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