gpt4 book ai didi

ios - UITextField 不会关闭

转载 作者:行者123 更新时间:2023-11-28 11:59:25 25 4
gpt4 key购买 nike

我目前正在制作一个包含 UITextField 的应用程序,它们一直按预期工作,但是当我最近去测试我的应用程序时,我注意到在我按下 UITextField 后键盘没有消失。在我点击离开或按下回车键之前它就像正常关闭一样,但现在我无法做任何事情让它关闭?

这是我当前的 ViewController 代码:

import UIKit

class CreateNewBudgetVC: UIViewController {
@IBOutlet weak var grossIncomeVal: UITextField!
@IBOutlet weak var locationVal: UITextField!
@IBOutlet weak var federalExptVal: UITextField!
@IBOutlet weak var stateExptVal: UITextField!
@IBOutlet weak var deductionsList: UITableView!

@IBAction func cancelPressed(_ sender: Any) {
performSegue(withIdentifier: "CancelSegue", sender: self)
}

@IBAction func generateBudgetPressed(_ sender: Any) {
let budget = Budget(grossIncome: Int(grossIncomeVal.text!)!)
performSegue(withIdentifier: "GenerateSegue", sender: budget)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let budget = sender as? Budget, let destination = segue.destination as? BudgetVC {
destination.budget = budget
}
}
}

最佳答案

就用

self.view.endEditing(true)

在上面定义的操作方法中,您要关闭键盘的位置。

例如

    @IBAction func cancelPressed(_ sender: Any) {
self.view.endEditing(true)
performSegue(withIdentifier: "CancelSegue", sender: self)
}

如果你想在键盘的 return tap 中返回键盘使用这个

class ViewController: UIViewController,UITextFieldDelegate

并实现委托(delegate)方法

func textFieldShouldReturn(_ textField: UITextField) -> Bool {   //delegate method
textField.resignFirstResponder()
return true
}

但不要忘记将 delegate 分配给 textfield

yourTextField.delegate = self in `viewDidLoad`

如果你想在外面点击时关闭键盘,只需像这样为你的 View 分配一个点击手势

In ViewDidload

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard (_:)))
self.view.addGestureRecognizer(tapGesture)

实现关闭功能

func dismissKeyboard (_ sender: UITapGestureRecognizer) {
self.view.endEditing(true)
}

关于ios - UITextField 不会关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50360315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com