IOS off keyboard 5 ways to exit the keyboardTurn from "http://blog.csdn.net/w88193363/article/details/24423635"Category: iphone2014-04-24 17:03 2197 people read reviews (0) favorite reports
1. Click outside the editing area (UIView)
2. Click outside the editing area (Uicontrol)
3. Use the button to make the keyboard
4. Input characters using judgment
5, about the problem of keyboard masking
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.
-(void) touchesended: (Nsset *) touches withevent: (Uievent *) Event {
if (![ Mytextview Isexclusivetouch]) {
[Mytextview Resignfirstresponder];
}
}
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.
-(Ibaction) Dismisskeyboard: (ID) Sender {
[Mytextview Resignfirstresponder];
}
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.
First, in the Viewdidload: event, register with Nsnotificationcenter and tell Nsnotificationcenter our Donebuttonshow: Method function.
-(void) Viewdidload {
[Super Viewdidload];
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (donebuttonshow:) Name: Uikeyboarddidshownotification Object:nil];
}
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.
-(void) Donebuttonshow: (nsnotification *) Notification {
Donebutton = [UIButton buttonwithtype:uibuttontyperoundedrect];
Donebutton.frame = CGRectMake (0, 228, 70, 35);
[Donebutton settitle:@ "Finish editing" forstate:uicontrolstatenormal];
[Donebutton addtarget:self Action: @selector (Hidekeyboard) forcontrolevents:uicontroleventtouchupinside];
[Self.view Addsubview:donebutton];
}
Finally, the Hidekeyboard when the push button is pressed: Method function, be sure to remember to remove the button in the function.
-(void) Hidekeyboard {
[Donebutton Removefromsuperview];
[Mytextview Resignfirstresponder];
}
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 adoption of the agreement, the TextView:shouldChangeTextInRange:replacementText in the agreement is then made: A method function, which is triggered when the character 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).
-(BOOL) TextView: (Uitextview *) TextView Shouldchangetextinrange: (nsrange) Range Replacementtext: (NSString *) Text {
if ([text isequaltostring:@ "\ n"]) {
[Mytextview Resignfirstresponder];
return NO;
}
return YES;
}
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.
-(void) Viewdidload {
[Super Viewdidload];
Mytextview.delegate = self;
}
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 Animation function of the Core Graphic, while the keyboard appears, move the editing area to solve the problem of masking.
IOS off keyboard 5 ways to exit the keyboard (GO)