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