4

I think I'm probably doing something stupid but I'm going to ask the question anyway. I cannot see the UITextFieldDelegate method textField being called. I've created a simple program with print statements showing all other delegate methods get called okay, but not this one. Here's the program code:

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    //MARK: Properties
    @IBOutlet weak var testTextField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        testTextField.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    //MARK: UITextFieldDelegate
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        print("textFieldShouldReturn")
        textField.resignFirstResponder()
        return true
    }

    func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
        print("textFieldShouldBeginEditing")
        return true
    }

   func textFieldDidBeginEditing(textField: UITextField) {
        print("textFieldDidBeginEditing")
        print("Leaving textFieldDidBeginEditing")
    }

    func textField(textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        print("textField")
        print("Leaving textField")
        return true
    }

   func textFieldDidEndEditing(textField: UITextField) {
        print("textFieldDidEndEditing")
        print("textField = \(textField.text)")
        print("Leaving textFieldDidEndEditing")        
    }
}

Ands that's it. I have a ViewController with a UITextField added in Interface Builder. Entering text shows the other delegate methods are being called okay. I'm using Xcode 7.3.1. Any help much appreciated and apologies up front if this is a dumb question - which I expect it is.

  • Did you connect the UITextField with the IBOutlet? – Ragul Sep 23 '16 at 12:15
  • Yes. I made the connection in IB. I am seeing all other delegate functions called - I guess I wouldn't have seen those if I'd not made the connection? – Colin Murgatroyd Sep 23 '16 at 13:00
  • Try marking the textField outlet as strong instead of weak (just remove the weak keyword), the reference could be becoming broken and hence the delegate is never called. Longshot but worth a try. – Jacob King Sep 23 '16 at 13:01
  • Tried that just now. No change I'm afraid. But I'm grateful for any long shots right now... – Colin Murgatroyd Sep 23 '16 at 13:04

2 Answers2

6

I think you are facing the issue because of the typo in the function.

This is what you are using:

func textField(textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {...}

This is how the function should be:

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {...}
Anton Tropashko
  • 5,486
  • 5
  • 41
  • 66
Ayazmon
  • 1,360
  • 8
  • 17
  • 1
    Okay thanks very much, it works now! I said I was probably asking a dumb question. I copied the function signature correctly from the API reference page, [link](https://developer.apple.com/reference/uikit/uitextfielddelegate/1619599-textfield). The Objective-C declaration has it correct as `shouldChangeCharactersInRange`. – Colin Murgatroyd Sep 23 '16 at 14:26
  • 1
    With Swift 5 (according to [this](https://stackoverflow.com/a/36464015/2016165) with Swift 3 & 4 too) you do have to use `shouldChangeCharactersIn`. – Neph Apr 05 '19 at 11:57
-1

just copy and paste this lines in your ViewDidLoad and have a try with this textField

let txtField: UITextField = UITextField(frame: CGRect(x: 0, y: 0, width: 400.00, height: 30.00));
    txtField.delegate = self
Ragul
  • 3,176
  • 3
  • 16
  • 32
  • No change. I started out this morning creating a text field programmatically and when I saw the textField method wasn't being called I created the test program above, and created the text field via IB to strip away as much unnecessary code as possible. Just tried it again though and still no change. – Colin Murgatroyd Sep 23 '16 at 13:43