In iOS app development, there are three types of view objects that open the virtual keyboard for input, but how to turn off the virtual keyboard without providing an automated method. This needs our own realization. These three types of view objects are Uitextfield,uitextview and Uisearchbar, respectively. Here are a few ways to turn off the virtual keyboard in Uitextfield.
The first method, using the method Textfieldshouldreturn in its delegate uitextfielddelegate: to close the virtual keyboard. Implement this method in the class where the Uitextfield view object is located, such as Birdnameinput.
C code
- -(BOOL) Textfieldshouldreturn: (Uitextfield *) TextField {
- if ((TextField = = Self.birdnameinput) | | (TextField = = self.locationinput)) {
- [TextField Resignfirstresponder];
- }
- return YES;
- }
This way, after you open the virtual keyboard in the input box birdnameinput, tapping the return key of the keyboard will automatically turn off the virtual keyboard.
The second method modifies the Birdnameinput property of return key to done, and then defines a method and the done key for the Do End on exit connection. This event is triggered by tapping the done key to turn off the virtual keyboard. The methods are defined as follows:
C code
- -(Ibaction) textfielddoneediting: (ID) sender
- {
- [Sender Resignfirstresponder];
- }
Both of these methods are to tap a key on the virtual keyboard to close it. This is a precise operation, and the finger is not like a mouse, and doing this is not easy. As a result, neither of these approaches is the best approach for the UI level. On the iphone or ipad screen, the virtual keyboard occupies a limited amount of space. Turn off the virtual keyboard by tapping an area other than the virtual keyboard.
The third method is to turn off the virtual keyboard by tapping an empty area outside the keyboard. Define a UITapGestureRecognizer object in the Viewdidload method of the View controller class that birdnameinput belongs to, and then assign it to its view.
C code
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (Dismisskeyboard)] ;
- [Self.view Addgesturerecognizer:tap];
- [Tap release];
Then define the method Dismisskeyboard called by the selector.
C code
- -(void) Dismisskeyboard {
- [Birdnameinput Resignfirstresponder];
- }
if there's more than one textfield on the screen, it's a bit of a hassle to list them one by one. Then modify the method as follows:
C code
- -(void) Dismisskeyboard {
- Nsarray *subviews = [Self.view subviews];
- For (id objinput in subviews) {
- if ([Objinput Iskindofclass:[uitextfield class]]) {
- Uitextfield *thetextfield = objinput;
- if ([Objinput Isfirstresponder]) {
- [Thetextfield Resignfirstresponder];
- }
- }
- }
- }
If the View object on this screen is complex, it is a matter of other things. This method is coded to create a new gesture object. You can also directly use the Interface Builder Graphical development tool, pull a gesture object into the View controller class in storyboard, and then set up a ibaction for this gesture object, which can be dismisskeyboard.
The fourth method is to turn off the virtual keyboard by tapping an empty area outside the keyboard. Drag a touch down event out of the screen view, the parent view of TextField, and connect to a method that turns off the virtual keyboard. If the view does not have a touch-down event, you can change the parent class of the view from UIView to UIButton. First define and implement a method Backgroundtap:.
C code
- -(Ibaction) Backgroundtap: (ID) sender
- {
- Nsarray *subviews = [Self.view subviews];
- For (id objinput in subviews) {
- if ([Objinput Iskindofclass:[uitextfield class]]) {
- Uitextfield *thetextfield = objinput;
- if ([Objinput Isfirstresponder]) {
- [Thetextfield Resignfirstresponder];
- }
- }
- }
- }
Then select the touch down event for the background view and connect Backgroundtap: you can. This allows you to turn off the virtual keyboard by tapping the area outside the virtual keyboard. These methods use the Resignfirstresponder method to turn off the virtual keyboard, and there are other methods.
The fifth method, using the Endediting: method, overrides this method in the view controller class in which it is located.
C code
- -(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event {
- [[Self view] endediting:yes];
- }
This method looks at the current view and its Subview hierarchy for the text field, which is currently the first responder. If it finds one, it asks that text field to resign as first responder. If The force parameter are set to YES, the text field is never even asked; It's forced to resign. However, if this screen is complex, there are many buttons in the area outside the virtual keyboard. Tapping these areas may tap these buttons so that the virtual keyboard cannot be turned off. If finding a blank area without a button is not easy and there are hidden view objects, it is difficult to turn off the virtual keyboard by tapping an area other than the virtual keyboard.
Sixth method, overriding Hittest:withevent: method to close the virtual keyboard
On the stackoverflow.com, someone summed it up. Say using Hittest:withevent: The method is the best and easiest solution.
I think the easiest (and best) the same-to-do-is-subclass your global view and use-hittest:withevent method to listen T o any touch.touches on keyboard aren ' t registered, so hittest:withevent are only called when you touch/scroll/swipe/pinch ... some Where else, then call [self endediting:yes]. This is better than using Touchesbegan because Touchesbegan be not called if you click on a button on top of the View. It is better than uitapgesturerecognizer which can ' t recognize a scrolling gesture for example. It is also better than using a dim screens because in a complexe and the dynamic user interface, you can ' t put the dim screen every where. Moreover, it doesn ' t block other actions, you don ' t need to tap twice to select a button outside (as in the case of a UI Popover). Also, it's better than calling [TextField Resignfirstresponder], because you could have many the text fields in screen, so this Works for all of them.
So I'm going to build a view class that inherits UIView. In this view class, override the Hittest:withevent: method to add the [self Endediting:yes] method.
C code
- -(UIView *) HitTest: (cgpoint) point withevent: (Uievent *) Event {
- UIView *result = [Super Hittest:point withevent:event];
- [Self Endediting:yes]
- return result;
- }
I have modified the view controller's main viewing owning class to this new view class. This will turn off the virtual keyboard if you tap anywhere on the screen. This method is the simplest and best way to turn off the virtual keyboard. Use good hittest:withevent: This method can also achieve a lot of very complex functions. The implementation of HitTest:withEvent:in Uiresponder does the following:
- It calls pointInside:withEvent:of self
- If The return was NO, hit Test:withEvent:returns Nil. The end of the story.
- If The return is YES, it sends HitTest:withEvent:messages to its subviews. It starts from the top- Level Subview, and continues to other views until a Subview returns a Non-nil object, or all subviews receive the message.
- If A Subview returns a Non-nil object in the first time, the first HitTest:withEvent:return s that object. The end of the story.
- If No Subview Returns a Non-nil object, the first hitTest:withEvent:returns self
This process repeats recursively, so normally the leaf view of the view hierarchy is returned eventually. However, might override hittest:withevent to do something differently. In many cases, overriding pointInside:withEvent:is simpler and still provides enough options to tweak event handling in Y Our application.
For more articles, please visit the code Farm 123