Home » Swift » Implementing UITextFieldDelegate with Swift

Implementing UITextFieldDelegate with Swift

Posted by: admin November 30, 2017 Leave a comment

Questions:

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? Here’s my class implementation.

class ViewController: UIViewController, UITextFieldDelegate
Answers:

Xcode 6 (Beta 1) is not currently supporting autocomplete for non-implemented protocol methods/properties (for Swift).

Your best bet is to <CMD> - click on the protocol that isn’t yet fully implemented to see what you’re missing.

Questions:
Answers:
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
}

Questions:
Answers:

A bit more swifty is …

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

Questions:
Answers:
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;
    }

Questions:
Answers:

// MARK:- —> Textfield 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, 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;
}

Questions:
Answers:

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;
    }
}

Questions:
Answers:

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.

Questions:
Answers:

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
    }
}

Questions:
Answers:

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.

Questions:
Answers:

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.