IOS Shutdown Virtual Keyboard method Rollup _ios

Source: Internet
Author: User

In iOS application development, there are three types of view objects open the virtual keyboard, input operations, but how to turn off the virtual keyboard, but does not provide an automated method. This needs to be achieved on our own. These three types of view objects are Uitextfield,uitextview and Uisearchbar respectively. Here are some ways to turn off the virtual keyboard in Uitextfield.

The first method, using the method in its delegate uitextfielddelegate Textfieldshouldreturn: To turn off the virtual keyboard.

Implement this method in the class where the Uitextfield view object, such as Birdnameinput, is located.

(BOOL) Textfieldshouldreturn: (Uitextfield *) TextField { 
if (TextField = = Self.birdnameinput) | | (TextField = = self.locationinput)) { 
[TextField resignfirstresponder]; 
} 
Return YES 
} 
-(BOOL) Textfieldshouldreturn: (Uitextfield *) TextField {
if (TextField = = Self.birdnameinput) | | (TextField = = self.locationinput)) {
[TextField resignfirstresponder];
}
Return YES
}

This way, when you open the virtual keyboard in the input box birdnameinput, clicking the return key of the keyboard automatically turns off the virtual keyboard.

The second method modifies the return key in the Birdnameinput property to done, and then defines a method and the did end on exit connection of the done key.

Turn off the virtual keyboard by tapping the done key to trigger this event.

The methods defined are as follows:

(ibaction) Textfielddoneediting: (ID) sender 
{ 
[sender Resignfirstresponder]; 
} 
-(Ibaction) textfielddoneediting: (ID) sender
{
[sender Resignfirstresponder];
}

Both of these methods tap a key on the virtual keyboard to close it. This is a precise operation, and the finger is not like a mouse, it is not easy to do this operation. So neither of these methods is the best approach to the UI level. On the iphone or ipad screen, the size of the virtual keyboard footprint is limited. Turn off the virtual keyboard by tapping an area other than the virtual keyboard.

The third way is to turn off the virtual keyboard by tapping a blank area outside the keyboard.

Define a UITapGestureRecognizer object in the Viewdidload method of the View controller class to which birdnameinput belongs, and assign it to its view.

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (Dismisskeyboard)] ; 
[Self.view Addgesturerecognizer:tap]; 
[Tap release];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (Dismisskeyboard)] ;
[Self.view Addgesturerecognizer:tap];
[Tap release];

Again, define the method Dismisskeyboard the selector calls.

(void) Dismisskeyboard { 
[birdnameinput resignfirstresponder]; 
} 
-(void) Dismisskeyboard {
[birdnameinput resignfirstresponder];
}

If there are multiple textfield on the screen, it's a bit of a hassle to list them one by one. Then modify the method as follows:

(void) Dismisskeyboard { 
Nsarray *subviews = [Self.view subviews]; 
For (ID objinput in subviews) { 
if ([Objinput Iskindofclass:[uitextfield class]]) { 
Uitextfield *thetextfield = ob Jinput; 
if ([Objinput Isfirstresponder]) { 
[Thetextfield resignfirstresponder]; 
}}} -(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 different story. 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 create a ibaction for this gesture object, the name can be dismisskeyboard.

The fourth method is to turn off the virtual keyboard by tapping a blank area outside the keyboard.

Drag the view on the screen, which is TextField's parent view, into a touch down event and connect to a method that closes the virtual keyboard. If the view does not touch the down event, you can modify the view's parent class from UIView to UIButton. First, define and implement a method Backgroundtap:.

(ibaction) Backgroundtap: (ID) sender 
{ 
Nsarray *subviews = [Self.view subviews]; 
For (ID objinput in subviews) { 
if ([Objinput Iskindofclass:[uitextfield class]]) { 
Uitextfield *thetextfield = ob Jinput; 
if ([Objinput Isfirstresponder]) { 
[Thetextfield resignfirstresponder]; 
}}} -(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 to connect Backgroundtap: you can. This allows you to turn off the virtual keyboard simply 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 .

(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event { 
[self view] endediting:yes]; 
} 
-(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" is currently the "the". If it finds one, it asks that text field to resign as a-responder. If The force parameter is set to YES, the text field is never even asked; It is forced to resign.

However, if the screen is complex, there are many buttons in the area outside the virtual keyboard. Tapping these areas may strike these buttons so that the virtual keyboard cannot be closed.

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 the area outside the virtual keyboard.

Sixth method, overriding Hittest:withevent: method to turn off the virtual keyboard

On the stackoverflow.com, someone summed it up. Using Hittest:withevent: Method is the best and easiest way to solve it.

I the easiest (and best) way to does this are to subclass your global view and use Hittest:withevent N to any touch. Touches on keyboard aren ' t registered and so hittest:withevent are only called when you touch/scroll/swipe/pinch ... somewhere else, then call [self endediting:yes]. This is better than using Touchesbegan because Touchesbegan are not called if you click on a button in top of the view. It is better than uitapgesturerecognizer which can ' t recognize a scrolling to gesture for example. It is also better than using a dim screens because in a complexe and dynamic user interface, with the can ' t put on dim screen every where. Moreover, it doesn ' t blocks other actions, you don ' t need to tap twice to select a button outside PopOver). Also, it's better than calling [TextField Resignfirstresponder], because you could have many text fields on screens, so this Works for all of them.

So I'm going to build a view class that inherits UIView. In this view class, overwrite the Hittest:withevent: method, and increase the [self endediting:yes] method.

(UIView *) HitTest: (cgpoint) point withevent: (Uievent *) event { 
UIView *result = [Super Hittest:point withevent:event ]; 
[Self Endediting:yes] 
return result; 
} 
-(UIView *) HitTest: (cgpoint) point withevent: (Uievent *) event {
UIView *result = [Super Hittest:point withevent: Event];
[Self Endediting:yes]
return result;
}

I'll modify the view controller's master-owned class to this new view class. This will turn off the virtual keyboard by tapping anywhere on the screen. This method is the simplest and the best way to turn off the virtual keyboard. Use good hittest:withevent: This method, but also can implement many very complex functions.

The implementation of HitTest:withEvent:in Uiresponder does the following:

Dances calls PointInside:withEvent:of self
If the return is NO, HitTest: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 Subvie WS receive the message.
If a subview returns a Non-nil object in the "the" The end of the story.
If no Subview returns a Non-nil object, the
This process repeats recursively, so normally the leaf view of the view hierarchy is returned eventually. However, you are might override hittest:withevent to do something differently. In many cases, overriding pointInside:withEvent:is simpler and still provides the options to enough the event tweak in Y Our application.

Above to introduce six kinds of iOS shut down the virtual keyboard method, we can according to individual needs to choose a better way for their own. At the same time, thank you very much for your support of cloud-dwelling community website!

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.