Implementing UITextFieldDelegate with Swift
Asked Answered
A

12

23

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
Appendage answered 11/6, 2014 at 20:19 Comment(2)
I don't think Xcode 6 is currently supporting autocomplete of non-implemented delegate methods in swiftParthena
Yes, auto complete is very wonky in Xcode 6...simply implement the delegate methods in your classGiacobo
P
7

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.

Parthena answered 11/6, 2014 at 20:23 Comment(0)
J
39
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
}
Jujitsu answered 7/7, 2014 at 6:18 Comment(1)
Note: in Swift 1.2 it should be: (textField: UITextField) -> Bool without the !Simper
S
23
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;
    }
Sopher answered 2/12, 2016 at 9:59 Comment(0)
O
18

A bit more swifty is ...

@IBOutlet weak var nameTF: UITextField! { didSet { nameTF.delegate = self } }
Ondrea answered 26/3, 2015 at 11:55 Comment(0)
A
18

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;
    }
}
Argot answered 20/6, 2017 at 13:4 Comment(0)
P
7

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.

Parthena answered 11/6, 2014 at 20:23 Comment(0)
I
7

// 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;
}
Incorruptible answered 29/7, 2016 at 10:25 Comment(0)
E
4

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
    }
}
Edieedification answered 13/5, 2017 at 3:52 Comment(0)
P
3

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.

Photooffset answered 20/6, 2014 at 17:30 Comment(0)
E
3

Swift 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
    }
}
Edieedification answered 15/5, 2018 at 5:46 Comment(2)
Do you need to use the extension? I achieved the same result by implementing textFieldShouldReturn inside the YourNameViewControllerDaedal
It's just a habitEdieedification
B
1

I spent a long night trying to fix this and the problem was that my coworker implemented all the logic in textFieldShouldBeginEditing instead of textFieldDidBeginEditing and sometimes textFieldShouldBeginEditing was not called when I used the first responder between TextFields ...

Beech answered 6/11, 2020 at 2:49 Comment(0)
M
0

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.

Meadowlark answered 28/11, 2014 at 8:4 Comment(0)
J
0

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.

Johnnie answered 22/7, 2016 at 11:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.