gpt4 book ai didi

ios - 无法从 UITextField 中退出 FirstResponder

转载 作者:行者123 更新时间:2023-11-28 21:03:43 29 4
gpt4 key购买 nike

你知道(你能看到)为什么我不能退出 FirstResponder 并隐藏键盘吗?

仅供引用:Xcode 9

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var label: UILabel!
@IBOutlet weak var text: UITextField!

override func viewDidLoad() {
super.viewDidLoad()

self.text.delegate = self
let tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapFunction))
label.isUserInteractionEnabled = true
label.addGestureRecognizer(tap)
}

var keyboardInputEnabled = true
@objc func tapFunction(sender:UITapGestureRecognizer) {
if self.keyboardInputEnabled {
self.text.becomeFirstResponder()
}else{
self.view.endEditing(true)
self.text.resignFirstResponder()
}
self.keyboardInputEnabled = !self.keyboardInputEnabled
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
return self.keyboardInputEnabled
}

func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
return self.keyboardInputEnabled
}

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return self.keyboardInputEnabled
}


override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

}

最佳答案

因为 textFieldShouldEndEditing 在您第二次运行 tapFunction 时返回 false,这会阻止它结束编辑模式。

取自this

Normally, you would return true from this method to allow the text field to resign the first responder status. You might return false, however, in cases where your delegate detects invalid contents in the text field. Returning false prevents the user from switching to another control until the text field contains a valid value.

关于ios - 无法从 UITextField 中退出 FirstResponder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46932627/

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