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?
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.
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
}
}
A bit more swifty is ...
@IBOutlet weak var nameTF: UITextField! { didSet { nameTF.delegate = self } }
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 ...
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;
}
// 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;
}