[ios] hide keyboard

1. Press return to hide

refer : http://www.2cto.com/kf/201206/134976.html

.h:

@interface MyViewController :UIViewController <UITextFieldDelegate>

Associated delegate: 

The first method: .m : viewDidLoad:

textField.delegate =self;

 The second method:

Drag controls in storyboard to point to delegate

 

Add code:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
   [textField resignFirstResponder];
   return YES;
 }

 

Click on the blank space:

the first method:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.textfield resignFirstResponder];
}

 

The second method:

1. Add Tap Gesture Recongnizer to the main view

2. Add on .m

-(IBAction)backgroundTap:(id)sender{
    [self.loginName resignFirstResponder];
    [self.password resignFirstResponder];
}

 3. Open the storyboard, open the Association Inspector, find the backtroudTap in Received Actions, and pull it to the Tap Gesture Recongnizer dock icon to complete the link.

(You can also double-click in .m to open storyboard and drag Tap Gesture Recongnizer to src to automatically generate control actions)

 

refer to:

1.http://www.jianshu.com/p/532d12375e9a

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326400232&siteId=291194637