Title: textField delegates are not being called
Tags: ios;xcode;swift3;uitextfield;uitextfielddelegate
Question: Below mentioned are my textField delegate methods, I am using IQKeyBoardSwift for inteligent keyboard.
I tried removing my keyboard but still I am not receiving call on any of my method accept 'begins touch'
func textFieldShouldReturn(textField: UITextField) -&gt; Bool {
if textField == textField {
textFIeld2.becomeFirstResponder()
}else if textField == textFIeld2 {
textField3.becomeFirstResponder()
}else if textField == textField3 {
textFIeld4.becomeFirstResponder()
}else if textField == textFIeld4 {
textFIeld4.resignFirstResponder()
return true
// Resign first reponder on touch
override func touchesBegan(_ touches: Set<UITouch&gt;, with event: UIEvent?) {
let touch = touches.first! as UITouch
let location = touch.location(in: view)
if !textField1.frame.contains(location) {
textField1.resignFirstResponder()
if !textFIeld2.frame.contains(location) {
textFIeld2.resignFirstResponder()
if !textField3.frame.contains(location) {
textField3.resignFirstResponder()
if !textFIeld4.frame.contains(location) {
textFIeld4.resignFirstResponder()
self.textField1.endEditing(true)
self.textFIeld2.endEditing(true)
self.textField3.endEditing(true)
self.textFIeld4.endEditing(true)
//MARK: UITextFieldDelegate
func textFieldShouldBeginEditing(textField: UITextField) -&gt; Bool {
print("textFieldShouldBeginEditing")
return true
func textFieldDidBeginEditing(textField: UITextField) {
print("textFieldDidBeginEditing")
print("Leaving textFieldDidBeginEditing")
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -&gt; Bool {
let newString = NSString(string: textField.text!).replacingCharacters(in: range, with: string)
if(textField == textField1)
if(newString.characters.count) == 1 {
print(textField1)
textFIeld2.becomeFirstResponder()
return false
}
if(textField == textFIeld2)
if(newString.characters.count) == 1{
textField3.becomeFirstResponder()
return false
}
if(textField == textField3)
if(newString.characters.count) == 1{
textFIeld4.becomeFirstResponder()
return false
}
if(textField == textFIeld4)
if(newString.characters.count) == 1{
buttonOutletSubmitCode.isHidden = false
resignFirstResponder()
return false
}
return true
I want my cursor to jump to next textfield after receiving an input of 1 character. Unfortunately That is not happening.
override func viewDidLoad() {
super.viewDidLoad()
textField1.delegate = self as? UITextFieldDelegate
textFIeld2.delegate = self as? UITextFieldDelegate
textField3.delegate = self as? UITextFieldDelegate
textFIeld4.delegate = self as? UITextFieldDelegate
This is my viewDidLoad
Comment: @RajatAttri replace you code.

Comment: all the IBOutlets are connected

Comment: No, it's not called. I want to restrict user from entering more than one character in textfield

Comment: Begins touch only

Comment: textFieldShouldBeginEditing is this method called ?

Comment: any delegate method called ?

Comment: add protocol UITextFieldDelegate and try to replace      textField1?.delegate = self as UITextFieldDelegate

Comment: for check for juum to nextfield : http://stackoverflow.com/a/18711677/3901620

Comment: @RajatAttri check this for length : http://stackoverflow.com/a/1773257/3901620

Comment: @RajatAttri first you need to set your view controller as the text field delegate `class ViewController: UIViewController, UITextFieldDelegate {`

Comment: next set the viewController as the textField delegate in the method viewDidLoad `textField1.delegate = self` `textFIeld2.delegate = self` and so on

Here is the accepted answer: Adding :
Add protocol UITextFieldDelegate and try to replace textField1?.delegate = self as UITextFieldDelegate
Replace
This doesn't work:
func textFieldDidBeginEditing(textField: UITextField) {
This works:
func textFieldDidBeginEditing(_textField: UITextField) {
For jump textField
see :  https://stackoverflow.com/a/18711677/3901620
Comment for this answer: did you check in simulator ?

Here is another answer: Adding textFiled delegate :
class ViewController: UIViewController  , UITextFieldDelegate{
Yes, Its Not Call because of your delegate is OLD swift version. Replace it Swift 3 code.
func textFieldShouldBeginEditing(_ textField: UITextField) -&gt; Bool{
if textField == textField {
textFIeld2.becomeFirstResponder()
}else if textField == textFIeld2 {
textField3.becomeFirstResponder()
}else if textField == textField3 {
textFIeld4.becomeFirstResponder()
}else if textField == textFIeld4 {
textFIeld4.resignFirstResponder()
return true
Comment for this answer: @RajatAttri textField connection with delegate in storyboard.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 11, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:05/01/17', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<ios><xcode><swift3><uitextfield><uitextfielddelegate>', 'Title': 'textField delegates are not being called', 'ViewCount': 2305, 'id': 43714883, 'user_AboutMe': None, 'user_AccountId': 7353280.0, 'user_DisplayName': 'Rajat Attri', 'user_Reputation': 73.0, 'user_Views': 10.0, 'user_id': 5598732.0}}