Implementing UITextFieldDelegate with Swift

前端 未结 12 1218
后悔当初
后悔当初 2020-12-08 08:11

I have my ViewController class which implements UITextFieldDelegate. I have no auto complete for the funcs such as textFieldShouldBeginEditing. Is this a bug in XCode 6?

相关标签:
12条回答
  • 2020-12-08 08:36

    Swift 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
        }
    }
    
    0 讨论(0)
  • 2020-12-08 08:41

    In my case by mistake I have added the delegate methods outside of the scope of the class implementation in swift and that restricts the delegate methods to be called.

    0 讨论(0)
  • 2020-12-08 08:44

    While using Swift Version 3.1 with the outlets of UITextFields, do mark the changes.

    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;
        }
    }
    
    0 讨论(0)
  • 2020-12-08 08:45

    I found a little work-around. Just go to file inspector and set type to Objective-C while you are editing the file. Auto-completion presents you Swift options.

    Just switch the type back to Swift when you build.

    0 讨论(0)
  • 2020-12-08 08:51

    I had a semicolon by mistake add to the gesture statement, which was responsible for calling view.endEditing(true) which in turns calls the delegate methods such as textFieldShouldBeginEditing. Interesting swift does not show any compile time or run time errors for semicolons added sometimes, After removing the semicolon everything just works fine.

    0 讨论(0)
  • 2020-12-08 08:52
    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
    }
    
    0 讨论(0)
提交回复
热议问题