简体   繁体   中英

iOS swift delegate with more than 1 uitextfield in a uiview

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

Easiest way is to know what text field to use in delegate methods. Ie 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.

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

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")
           
       }
    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM