ios - 在 Swift 上单击文本字段时隐藏 UIButton

标签 ios swift uibutton uitextfield

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/

相关文章:

ios - 跨 ViewController 保存信息

ios - 尝试通过 JSON 将自定义类发送到 .NET Web 服务时,JSON 写入错误中的类型无效

ios - Swift 闭包从函数返回

ios - 我如何通过单击按钮来控制标签栏项目?

ios - 单击 UICollectionViewCell 按钮并更改单元格上的文本

iOS 圆形 slider

ios - NSSet Iteration 比 NSMutableArray Iteration 消耗更多时间,NSArray Iteration 比 NSMutableArray Iteration 消耗更多时间

ios - UIStoryboardSegue Modal Form Sheet 首先填满整个屏幕

ios - 无法使用服务 UUID 发现外围设备

ios - 想要在单击相同的 UIButton 时从 UITableViewCell 更改正确的 UIButton 图像