UITextFieldDelegate extension function does not triggers, why? - swift

I have SequencedTextFields protocol, which contains sequence of text fields. When user taps Return button on keyboard, current text field should resign first responder and next text field in the sequence should become first responder. And it works good, when I'm using direct implementation of UITextFieldDelegate protocol for view controller:
extension MyViewController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
nextInSequence(after: textField)?.becomeFirstResponder()
return true
}
}
But, when I'm trying to use default implementation, it does not triggers ever:
extension UITextFieldDelegate where Self: SequencedTextFields {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
nextInSequence(after: textField)?.becomeFirstResponder()
return true
}
}
What could be the reason? Or I've missed something?
UPDATE:
My view controller defining:
final class MyViewController: UIViewController, UITextFieldDelegate, SequencedTextFields
Setting up the delegate for the text fields:

The reason why your implementation is not working is because you are trying to extend an interface (UITextFieldDelegate) instead of a class, that's why it works when you use UIViewController instead.
What you can do is to create a custom class SequencedTextField that extends UITextField. Add a custom delegate (that I called sequencedDelegate that represents the class that implements your SequencedTextFields protocol.
Extend SequencedTextField to implement UITextFieldDelegate with your default implementation.
On MyViewController, set up your SequencedTextField delegate with the viewController itself.
At the end it should look something like this:
protocol SequencedTextFields: class {
func nextInSequence(after: UITextField) -> UITextField?
}
class SequencedTextField: UITextField {
weak var sequencedDelegate: SequencedTextFields?
}
extension SequencedTextField: UITextFieldDelegate {
public func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
sequencedDelegate?.nextInSequence(after: textField)?.becomeFirstResponder()
return true
}
}
class MyViewController : UIViewController, SequencedTextFields {
var textField = SequencedTextField()
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = textField
textField.sequencedDelegate = self
}
func nextInSequence(after: UITextField) -> UITextField? {
// your view controllers nextInSequence implementation here
}
}

What is wrong with your code?
The first extension is the extension of your view controller MyViewController, that's why it is working. But the second one is the extension of UITextFieldDelegate. Those are completely two different things.
Not clear what you want to achieve.
This is how you can use your custom protocol
If you want to make one CustomProtocol for your special UITextField you can do like this.
CustomTextFieldProtocol
Declear the protocol
protocol SequencedTextFields: UITextFieldDelegate {
func checkSomeThing(_ textField: UITextField) -> Bool
}
Set the delegate
self.textField.delegate = self
ViewController Extension to implement your Delegate methods
extension ViewController: SequencedTextFields {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
nextInSequence(after: textField)?.becomeFirstResponder()
return true
}
func checkSomeThing(_ textField: UITextField) -> Bool {
print("Check here")
return true
}
}

Related

Add uipickerview stored in object to viewcontroller's view

Many people on SO suggest using a uipickerview and a uitextfield to make a dropdown-like interface. I having a pickerview stored in an object. How can I then add this to a viewcontroller's view when the textfield is tapped?
Instead of storing the pickerview, store only the datasource in the dropdown class. Make the ViewController the delegate of the dropdown class. When the textfield begins editing, call a delegate method to set the datasource of the pickerview the the viewcontroller to the dropdown
Dropdown Delegate protocol:
protocol DropdownDelegate : class {
func activateDropdown(dropdown: Dropdown)
}
Dropdown class:
class Dropdown: UITextField, UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate {
var dataSouce : String = []
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == self {
if(dropdownDelegate != nil) {
dropdownDelegate?.activateDropdown(dropdown: self)
textField.endEditing(true)
}
}
}
//rest of delegate methods
}
ViewController class
class ViewController: UIViewController, DropdownDelegate {
#IBOutlet weak var pickerView: UIPickerView!
func activateDropdown(dropdown: Dropdown) {
self.pickerView.delegate = dropdown
self.pickerView.dataSource = dropdown
}
func deactivateDropdown() {
changeDropdownStatus(status: true)
self.pickerView.isHidden = true
}
}

return key pressed inside uitableviewcell swift

I have a textfield in each cell of my tableview. I want to quit keyboard when users pressed on the return key. I tried:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
But it doesn't work and I can't seem to find a away to reference the textfield inside the cell of my tableview. Please help, thanks!
I assume that you have created IBOutlets of your textfield in your cell class.
Give your textfield delegate to self in cellForRow datasource method of UITableView
cell.yourTextField.delegate = self
You can give delegate form storyboard.
You should let your custom UITableViewCell class to confirms to UITextFieldDelegate:
class TableViewCell: UITableViewCell, UITextFieldDelegate {
// ...
}
In awakeFromNib you should your textfields delegate to the cell class:
override func awakeFromNib() {
// ...
textField.delegate = self
// ...
}
Finally, implement textFieldShouldReturn:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
Hope that helped.

How to add text to an active UITextField

I have a couple of UITextField's and one UIButton. If I tap on the UIButton, I want to place some static text in the active UITextField.
So something like:
#IBAction func buttonEen(sender: UIButton) {
'active textField'.text = "static text"
}
But how do I determine which UITextField is the active UITextField and how do I reach it?
To write text in last active UITextField you have to make your UIViewController a delegate of UITextField
ex:
class ViewController: UIViewController, UITextFieldDelegate
declare a activeField variable
ex:
var activeField: UITextField?
then implements textFieldDidBeginEditing
ex:
func textFieldDidBeginEditing(textField: UITextField)
{
activeField = textField
}
So your button function will be something like
#IBAction func buttonEen(sender: UIButton) {
if activeField
{
activeField.text = "static text"
}
}
First you need to create your textfield delegate (probably in 'ViewDidLoad()') :
activeTxtField.delegate = self
Then you need to implement the textField delegate function:
extension 'YourVC': UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == activeTxtField {
// Do whatever you want
// e.g set textField text to "static text"
}
}
}

How to dismiss keyboard for UITextView

The following triggers when the Done button is clicked on the keyboard. It goes into the conditional and returns false. However, the keyboard remains on the screen.
class MyViewController: UIViewController, UITextViewDelegate{
func textView(textView: UITextView!, shouldChangeTextInRange: NSRange, replacementText: NSString!) -> Bool {
if(replacementText.isEqualToString("\n")) {
textBox.resignFirstResponder()
return false
}
return true
}
textBox is a delegate for the view. I have connected this through Interface Builder. Any ideas why it isn't dismissing the keyboard?
First of all please ensure that your My ViewController is a delegate of the textField,
And then, on the method shouldChangeTextInRange use the textView that is passed in the delegate method instead of your property textBox.
For example:
class MyViewController: UIViewController, UITextViewDelegate{
func viewDidLoad(){
super.viewDidLoad()
textBox.delegate = self
}
func textView(textView: UITextView!, shouldChangeTextInRange: NSRange, replacementText: NSString!) -> Bool {
if(replacementText.isEqualToString("\n")) {
textView.resignFirstResponder()
return false
}
return true
}

Is there any way to make UITextFieldDelegate more dynamic in swift?

I nearly have a UITextFieldDelegate in a UIViewController,and I make
import UIKit
class testViewController: UIViewController,UITextFieldDelegate{
#IBOutlet weak var textName: UITextField!
override func viewDidLoad(){
super.viewDidLoad()
textName.delegate = self
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
return true
}
}
it works very well.
but my question is, when i have two, or three UITextFields, and have different function,what should i do? Is there any way to make UITextFieldDelegate more dynamic in swift? just like java do? I remember that java may do like that:
textName.delegate = UITextFieldDelegate{
func textFieldShouldReturn(textField: UITextField) -> Bool {
return true
}
}
textAddress.delegate = UITextFieldDelegate{
func textFieldShouldReturn(textField: UITextField) -> Bool {
return true
}
}
The delegate methods take a parameter that identifies the text field for which the demarcate methods are being called. You can use this to provide different behaviours for different text fields.