简体   繁体   English

如何在 Swift 的子视图中访问 UITextfield?

[英]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.

相关问题 如何在Swift中引用UITextField的文本? - How can I reference the text of a UITextField in Swift? 如何通过swift将底线添加到子视图中的多个UITextField? - How to add bottom line to multiple UITextField in subviews by swift? Swift:如果用户达到最大字符,如何移动到下一个 UITextField - Swift: How to move to next UITextField if the user reach max char 如何在Swift中更改UIAlertController中UITextField的高度? - How can I change the height of UITextField in UIAlertController in Swift? 我如何基于用户输入快速创建UITextField - how can i create UITextField based on a user input in swift Swift-如何强制用户在UITextField中仅导入整数? - Swift - How can I force users to import only integer in UITextField? 如何在Swift中将光标移动到UITextField的开头? - How can I move the cursor to the beginning of a UITextField in Swift? Swift UITextfield 委托 Function “shouldChangeCharactersIn” - 如何停止编辑 UITextfield? - Swift UITextfield Delegate Function "shouldChangeCharactersIn" - How can I stop editing for a second UITextfield? iOS Swift如何在删除超级视图或在子视图中关闭它之间进行检查 - IOS swift how can I Check between remove super view or dismissing it in subviews 如何将对象转发到单独的子视图? - How can i forward objects to separate subviews?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM