【问题标题】:Implementing UITextFieldDelegate with Swift用 Swift 实现 UITextFieldDelegate
【发布时间】:2014-06-11 20:19:24
【问题描述】:

我有实现 UITextFieldDelegate 的 ViewController 类。我没有自动完成诸如 textFieldShouldBeginEditing 之类的功能。这是 XCode 6 中的错误吗?这是我的类实现。

class ViewController: UIViewController, UITextFieldDelegate

【问题讨论】:

  • 我认为 Xcode 6 目前不支持 swift 中未实现的委托方法的自动完成
  • 是的,自动完成在 Xcode 6 中非常不稳定...只需在您的类中实现委托方法

标签: ios delegates swift


【解决方案1】:
class ViewController: UIViewController,UITextFieldDelegate  //set delegate to class 

@IBOutlet var txtValue: UITextField             //create a textfile variable 

override func viewDidLoad() {
    super.viewDidLoad()
    txtValue.delegate = self                  //set delegate to textfile
}


func textFieldDidBeginEditing(textField: UITextField!) {    //delegate method

}

func textFieldShouldEndEditing(textField: UITextField!) -> Bool {  //delegate method
    return false
}

func textFieldShouldReturn(textField: UITextField!) -> Bool {   //delegate method
  textField.resignFirstResponder()

    return true
}

【讨论】:

  • 注意:在 Swift 1.2 中它应该是:(textField: UITextField) -> Bool 没有 !
【解决方案2】:
Swift 3.0.1

 // UITextField Delegates
    func textFieldDidBeginEditing(_ textField: UITextField) {
    }
    func textFieldDidEndEditing(_ textField: UITextField) {
    }
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        return true;
    }
    func textFieldShouldClear(_ textField: UITextField) -> Bool {
        return true;
    }
    func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
        return true;
    }
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        return true;
    }
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder();
        return true;
    }

【讨论】:

    【解决方案3】:

    稍微快一点是...

    @IBOutlet weak var nameTF: UITextField! { didSet { nameTF.delegate = self } }
    

    【讨论】:

      【解决方案4】:

      在使用 Swift 3.1 和 UITextFields 的出口时,请标记更改。

      import UIKit
      
      class LoginViewController: UIViewController, UITextFieldDelegate {
       @IBOutlet var txtUserID: UITextField!
       @IBOutlet var txtPwd: UITextField!
       override func viewDidLoad() {
          super.viewDidLoad()
      
          txtUserID.delegate = self
          txtPwd.delegate = self
       }
       // UITextField Delegates
          func textFieldDidBeginEditing(_ textField: UITextField) {
              print("TextField did begin editing method called")
          }
          func textFieldDidEndEditing(_ textField: UITextField) {
              print("TextField did end editing method called\(textField.text!)")
          }
          func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
              print("TextField should begin editing method called")
              return true;
          }
          func textFieldShouldClear(_ textField: UITextField) -> Bool {
              print("TextField should clear method called")
              return true;
          }
          func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
              print("TextField should end editing method called")
              return true;
          }
          func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
              print("While entering the characters this method gets called")
              return true;
          }
          func textFieldShouldReturn(_ textField: UITextField) -> Bool {
              print("TextField should return method called")
              textField.resignFirstResponder();
              return true;
          }
      }
      

      【讨论】:

        【解决方案5】:

        Xcode 6(Beta 1)目前不支持未实现的协议方法/属性(用于 Swift)的自动完成。

        您最好的选择是在尚未完全实施的协议上<CMD> - click,看看您缺少什么。

        【讨论】:

          【解决方案6】:

          // MARK:- ---> 文本字段代表

          func textFieldDidBeginEditing(textField: UITextField) {
          
              print("TextField did begin editing method called")
          }
          
          func textFieldDidEndEditing(textField: UITextField) {
          
              print("TextField did end editing method called\(textField.text)")
          }
          
          func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
          
              print("TextField should begin editing method called")
              return true;
          }
          
          func textFieldShouldClear(textField: UITextField) -> Bool {
          
              print("TextField should clear method called")
              return true;
          }
          
          func textFieldShouldEndEditing(textField: UITextField) -> Bool {
              print("TextField should end editing method called")
              return true;
          }
          
          
          func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
              print("While entering the characters this method gets called")
              return true;
          }
          
          
          func textFieldShouldReturn(textField: UITextField) -> Bool {
          
              print("TextField should return method called")
              textField.resignFirstResponder();
              return true;
          }
          

          【讨论】:

            【解决方案7】:

            斯威夫特 3

               @IBOutlet weak var yourNameTextfield: UITextField! {
                    didSet {
                        yourNameTextfield.delegate = self
                    }
                }
            
            extension YourNameViewController: UITextFieldDelegate {
                func textFieldDidBeginEditing(_ textField: UITextField) {
            
                }
                func textFieldDidEndEditing(_ textField: UITextField) {
            
                }
                func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
                    return true
                }
                func textFieldShouldClear(_ textField: UITextField) -> Bool {
                    return true
                }
                func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
                    return true
                }
                func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
                    return true
                }
                func textFieldShouldReturn(_ textField: UITextField) -> Bool {
                    textField.resignFirstResponder();
                    return true
                }
            }
            

            【讨论】:

              【解决方案8】:

              我找到了一些解决方法。只需在编辑文件时转到文件检查器并将类型设置为 Objective-C。自动完成为您提供 Swift 选项。

              只需在构建时将类型切换回 Swift。

              【讨论】:

                【解决方案9】:

                斯威夫特 4:

                @IBOutlet weak var yourNameTextField: UITextField! {
                        didSet {
                            yourNameTextField.delegate = self
                        }
                }
                
                
                extension YourNameViewController: UITextFieldDelegate {
                    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
                        switch textField {
                        case yourNameTextField:
                            yourNameTextField.resignFirstResponder()
                        default:
                            break
                        }
                        return true
                    }
                
                    func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
                        return true
                    }
                }
                

                【讨论】:

                • 您需要使用extension吗?我通过在 YourNameViewController 中实现 textFieldShouldReturn 实现了相同的结果
                • 这只是一种习惯
                【解决方案10】:

                我花了一个漫长的夜晚试图解决这个问题,问题是我的同事在 textFieldShouldBeginEditing 而不是 textFieldDidBeginEditing 中实现了所有逻辑,有时当我在 TextFields 之间使用第一响应者时没有调用 textFieldShouldBeginEditing .. .

                【讨论】:

                  【解决方案11】:

                  在我的例子中,我错误地在 swift 中添加了类实现范围之外的委托方法,这限制了要调用的委托方法。

                  【讨论】:

                    【解决方案12】:

                    我在手势语句中错误地添加了一个分号,该语句负责调用 view.endEditing(true),而后者又调用了诸如 textFieldShouldBeginEditing 之类的委托方法。有趣的 swift 不会显示任何编译时或运行时错误,有时会添加分号,删除分号后一切正常。

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2015-12-09
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多