[英]Crash on self.view.endEditing with UITextField in tableviewheader
[英]UITextField's resignFirstResponder is not working neither working self.view.endEditing()
我面對textField的一個荒謬的問題,我有兩個文本域,即tfA和tfB,我已經為那些文本字段設置了委托,我想要的是,如果我點擊tfA然后它應該打印一些東西,它就是打印如果我點擊tfB它應該出現在鍵盤上,它也運行良好,但是當我再次點擊tfA然后它應該打印一些東西,鍵盤應該根據那里給出的條件解散,但鍵盤並沒有在那里解雇self.view.endEditing(true)
在這里不起作用。 代碼在下面給出屏幕截圖,我在這里做錯了什么?
代碼:Swift 3
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var tfA: UITextField!
@IBOutlet weak var tfB: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
tfA.delegate = self
tfB.delegate = self
}
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == tfA{
print("TFA Clicked")
textField.resignFirstResponder()
self.view.endEditing(true)
}else{
tfB.becomeFirstResponder()
}
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
截圖
嘗試這個
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
if textField == tfA
{
self.view.endEditing(true)
tfaAction()
return false
}
else
{
return true
}
}
func tfaAction(){
}
刪除textFieldDidBeginEditing
方法,將其替換為textFieldShouldBeginEditing
:
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
if textField == tfA{
print("TFA Clicked")
self.view.endEditing(true)
return false
}else{
return true
}
}
Swift 4只檢查這個delegate
是否存在然后它應該return true
`func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {`
//MARK:- Textfield Delegates //This is mandatory
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.view.endEditing(true)
return true
}
//MARK:- This Delegate is option but if this is exist in your code then return type shoud be true
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
return true
}
運行textfield.resignFirstResponder()
所以一切都會像
textfield.resignFirstResponder()
self.view.endEditing(true)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.