UITextField的resignFirstResponder无法正常工作self.view.endEditing()

Abh*_*tra 7 iphone uitextfield ios resignfirstresponder swift3

我面对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.
    }


}
Run Code Online (Sandbox Code Playgroud)

截图

在此输入图像描述

Raj*_*r R 17

试试这个

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
   if textField == tfA
   {
       tfaAction()
       return false
   }
   else
   {
       return true
   }
}
func tfaAction(){

 }
Run Code Online (Sandbox Code Playgroud)


Cra*_*itt 6

删除您的textFieldDidBeginEditing方法,将其替换为textFieldShouldBeginEditing:

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
  if textField == tfA{
    print("TFA Clicked")
    self.view.endEditing(true)
    return false
  }else{
    return true
  }
}
Run Code Online (Sandbox Code Playgroud)