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