Swift 的新手,制作一个简单的待办事项应用程序。
我正在尝试获取它,以便在单击 UI TextField 时隐藏某个按钮。只有当用户在键入任务后按下回车键时,按钮才会出现。
我为我的文本字段设置了一个 IBAction,以便在单击时隐藏 UIButton,但它不起作用:
@IBAction func textFieldClicked(_ sender: Any) {
self.cellButton.isHidden = true
}
我已经设置了我的 textfieldshouldreturn 函数,当用户按下 enter 时,像这样:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.cellButton.isHidden = false
self.cellButton.isUserInteractionEnabled = true
textField.resignFirstResponder()
return true
}
文本字段的 IBAction 是否仅在按下 enter 时响应?我试过弄乱标志,但这也没有用。
感谢任何帮助。
最佳答案
为此您不需要 IBAction
。相反,实现另一个 UITextFieldDelegate
方法 - textFieldDidBeginEditing
。
func textFieldDidBeginEditing(_ textField: UITextField) {
self.cellButton.isHidden = true
}
当文本字段成为第一响应者时调用此委托(delegate)。
稍微不相关的一点是,最好从您的 textFieldShouldReturn
方法返回 false
而不是 true
。
关于ios - 在 Swift 上单击文本字段时隐藏 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41527246/