【问题标题】:UITextField's resignFirstResponder is not working neither working self.view.endEditing()UITextField 的 resignFirstResponder 不起作用 self.view.endEditing()
【发布时间】:2017-04-05 13:20:45
【问题描述】:

我在 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.
    }


}

截图

【问题讨论】:

  • 删除textField.resignFirstResponder() 并尝试。当您从 tfB 回来时,它的键盘处于活动状态。所以self.view.endEditing(true) 足以关闭视图中活动的键盘。
  • 如果您不希望 tfA 可编辑,为什么不干脆做tfA.isEnabled = false
  • @ron27 我试过了,但没用。
  • @RajeshkumarR 我需要对 tfA 采取行动,这就是为什么我不能让它成为假。
  • 创建一个没有标题且位置和大小与 tfA 相同的按钮。

标签: ios iphone swift3 uitextfield resignfirstresponder


【解决方案1】:

试试这个

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

 }

【讨论】:

  • 感谢您的努力,但它也不起作用,我的问题是如果您点击 tfB 键盘将显示,如果您点击 tfA 键盘应该被解雇。此外,如果您先单击 tfA,则不应显示键盘。但在此我需要点击 tfA 的操作。
  • 感谢英雄@RajeshKumar R,在 5 秒内为我工作。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    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
        }
    

    【讨论】:

      【解决方案4】:

      运行textfield.resignFirstResponder()

      所以一切都会像

      textfield.resignFirstResponder()
      self.view.endEditing(true)
      

      【讨论】:

        猜你喜欢
        • 2011-07-02
        • 2010-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多