[英]How can I reach UITextfield in subviews in Swift?
Here is the objects hierarchy in my ViewController in Main.Storyboard这是 Main.Storyboard 中我的 ViewController 中的对象层次结构
Button same size with superview.按钮大小与超级视图相同。 I would like to close the keyboard with the button click action that's why I wrote this code scope:我想用按钮单击操作关闭键盘,这就是我编写此代码范围的原因:
@IBAction func btnCloseKeyboardClick(_ sender: UIButton) {
print("A")
for viewItem in self.view.subviews
{
print("B")
if (viewItem is UITextField)
{
print("C")
let tField = viewItem as! UITextField
tField.resignFirstResponder()
}
}
}
but it does not close the keyboard means I don't see print("C")
但它没有关闭键盘意味着我没有看到print("C")
I solved this problem like in this link but I would like to ask above situation also 我在这个链接中解决了这个问题,但我也想问上面的情况
如果您的视图中只有一个文本字段并希望为此关闭键盘,那么简单的方法是在按钮操作中调用view.endEditing(true)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.