简体   繁体   中英

How to dismiss the keyboard from the view?

I created the textfield programmatically for getting the value in it. Once i got the value in the textfield i wanted to dismiss the keyboard.

Could anyone help me in this with sample code?

Thanks in advance.

If you are asking about iOS' keyboard, then you can do this:

[textField resignFirstResponder];

When a UITextField gains focus, it is said to have "gained first responder status", meaning that it is the first UIResponder in the responder chain . What this means to you is that when you send the resignFirstResponder message to a UIResponder , the receiver will be popped off the responder chain and the next responder in the chain will gain first responder status.

[inputView resignFirstResponder];

Create a text field delegate method that would send resignFirstResponder message to text field in question when "return" button is pressed:

// in some method
[myTextField setDelegate:self];

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

This code is off top of my head so doublecheck the delegate method title.

have one button Action

 UIButton *keyboard=[UIButton buttonWithType:UIButtonTypeCustom];
keyboard.frame = CGRectMake(0,100,320,460);
[keyboard addTarget:self action:@selector(keyDown) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:keyboard];

in button action write this code

 -(void)keyDown{

[textfield resignFirstResponder];

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM