简体   繁体   English

如何从视图中解开键盘?

[英]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: 如果你问有关iOS的”键盘,那么你可以这样做:

[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 . UITextField获得焦点时,它被称为“已获得第一响应者状态”,这意味着它是响应者链中的第一个UIResponder 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. 这对您意味着什么,当您将resignFirstResponder消息发送到UIResponder ,接收者将从弹出的响应者链中弹出,并且链中的下一个响应者将获得第一响应者状态。

[inputView resignFirstResponder];

Create a text field delegate method that would send resignFirstResponder message to text field in question when "return" button is pressed: 创建一个文本字段委托方法,当按下“返回”按钮时,它将向有问题的文本字段发送resignFirstResponder消息:

// 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];

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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