4 ways iOS develops a hidden keyboard

Source: Internet
Author: User
Tags uicontrol uikit

4 ways iOS develops a hidden keyboard

The app is often used to hide the keyboard after keyboard input is complete. Here we summarize 4 ways to achieve our goal.

Method One--delegate Way

The first method of hiding the keyboard is called delegate three-step:
1. Follow (entrust/agent);
2. Call;
3. The agent is followed in the association header file (ViewController.h). The code is as follows:

#import <UIKit/UIKit.h>  @interface ViewController : UIViewController<UITextFieldDelegate> @end

In the implementation file (VIEWCONTROLLER.M), the method is called:

#pragma --mark textFieldDelegate  //调用delete方法,<UITextFieldDelegate>  -(BOOL)textFieldShouldReturn:(UITextField *)textField  {      [textField resignFirstResponder];//释放第一响应者      return YES; }

Where the code #pragma --mark textFieldDelegate is, the following method :textFieldShouldReturn marks it under the category Textfielddelegate. As shown in:

After the agent is followed and the method is created, the next step is to establish the connection.
With the controls selected, control-drag to the View controller and select Delegate. The connection is established.

After you make a connection, right-click the connected control and you see.

After the command+r is running, the keyboard can be hidden by typing the return key of the keyboard.

Method two--event response method:

Select the input control directly and drag the control to create an action and outlet.

In the implementation file, implement the action.

- (IBAction)didEnd:(id)sender {      [self.didTextfield resignFirstResponder];  }

Method three--click on the blank space to hide the keyboard

How to hide the keyboard when you click a blank position.
When the view is selected, change the class (originally UIView) to uicontrol--(because: Uicontrol is a subclass of UIView with all appearances and behaviors of the latter while firing events), then creates a touch up for the View object Inside a touch event, create a outlet for the input control.

The action implementation behavior.

- (IBAction)viewClicked:(id)sender{      //获取文本框对象后,退出第一响应者      [self.textField resignFirstResponder]; } 

Method four-artifice (not used)

Skillfully use UIButton. Fill the button controls full screen, remove the text above, set the background color to match the view color (here is the highlight effect, the button background color is set to red).
For the UIButton object, create a touch up inside action, create a outlet for the text box, and then implement the action,

- (IBAction)btnTap:(id)sender {      [self.textField resignFirstResponder];  }  

4 ways iOS develops a hidden keyboard

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.