5

I have an iOS app, with one UIView and three UITextField (more than 1) I would to understand what are the best practices for my class ViewController to manage the UITextField.

- class MainViewController: UIViewController, UITextFieldDelegate ?

I wonder that, because I have more than one UITextField and only one func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool

Victor Sigler
  • 23,243
  • 14
  • 88
  • 105
FREDERIC1405
  • 85
  • 1
  • 5

4 Answers4

15

Easiest way is to know what text field to use in delegate methods. I.e. you have 3 text fields: field1, field2, field3 and when delegate called you can detect what to do:

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    if textField == field1 {
        // do something
    } else if textField == field2 {
        // do something
    } else if textField == field3 {
        // do something
    }
  return true
}

Do not forget to make all field's delegate as self: field1.delegate = self etc.

In your case it will work fine.

If you want to know a better solution if you have much more fields (10, 20?) let me know and I'll update my answer.

Roman
  • 1,045
  • 1
  • 12
  • 33
anatoliy_v
  • 1,782
  • 15
  • 24
1

Best way to do this is using the tag attribute.

As seen on the Apple Docs:

- (void)textFieldDidEndEditing:(UITextField *)textField {

    switch (textField.tag) {
        case NameFieldTag:
            // do something with this text field
            break;
        case EmailFieldTag:
             // do something with this text field
            break;
        // remainder of switch statement....
    }
}

enum {
    NameFieldTag = 0,
    EmailFieldTag,
    DOBFieldTag,
    SSNFieldTag
};
dordio
  • 187
  • 2
  • 10
0
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    switch textField {
    case field1:
       // do something 
    case field2:
       // do something 
    case field3:
       // do something 
    }
  return true
}
0

This worked for me

import UIKit

class WeatherViewController: UIViewController,UITextFieldDelegate {

    @IBOutlet weak var conditionImageView: UIImageView!
    @IBOutlet weak var temperatureLabel: UILabel!
    @IBOutlet weak var cityLabel: UILabel!
    
    @IBOutlet weak var searchInputField: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.searchInputField.delegate=self
        // Do any additional setup after loading the view.
    }

    @IBAction func searchButtonClicked(_ sender: UIButton) {
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        if textField == searchInputField {
            print("Changes done in searchTextField")
        }
        searchInputField.resignFirstResponder() // it hides the keyboard
           performAction()
           print(" Inside textFieldShouldReturn")
           return true
       }
       
      
       func performAction() {
         print(" Perform action called")
           
       }
    
}
Quick learner
  • 10,632
  • 4
  • 45
  • 55