Wildteuk
Wildteuk

Reputation: 19

Get data writing in UITextField

I am creating a form for taking measurements and I therefore have several text files. I need to be able to retrieve what the user writes in the textField. I tried several methods but I can't find a way to get there. Can you give me a lead?

Here is my code:

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    var myTextField = UITextField()


    @IBOutlet weak var wristFlex: UITextField!
    @IBOutlet weak var wristExtension: UITextField!
    @IBOutlet weak var firstPhalanx: UITextField!
    @IBOutlet weak var secondPhalanx: UITextField!
    @IBOutlet weak var thirdPhalanx: UITextField!
    @IBOutlet weak var forearmLenght: UITextField!
    @IBOutlet weak var handLenght: UITextField!
    @IBOutlet weak var fingerLenght: UITextField!
    @IBOutlet weak var forearmCirecumference: UITextField!
    @IBOutlet weak var wristCirecumference: UITextField!
    @IBOutlet weak var fingerCirecumference: UITextField!
    @IBOutlet weak var handCirecumference: UITextField!
    @IBOutlet weak var validatedPressed: UIButton!




    override func viewDidLoad() {
        super.viewDidLoad()
        wristFlex.delegate = self
        wristExtension.delegate = self
        firstPhalanx.delegate = self
        secondPhalanx.delegate = self
        thirdPhalanx.delegate = self
        forearmLenght.delegate = self
        handLenght.delegate = self
        fingerLenght.delegate = self
        forearmCirecumference.delegate = self
        wristCirecumference.delegate = self
        fingerCirecumference.delegate = self
        handCirecumference.delegate = self

        NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)

        NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)

        NotificationCenter.default.addObserver(self, selector: #selector(textFieldTextDidChange), name: UITextField.textDidChangeNotification, object: nil)

    }

// Notifications:

      @objc  func textFieldTextDidChange(ncParam: NSNotification) {

        print("UItextFieldTextDidChange = \(ncParam)")

    }

    @objc func keyboardWillHide(notification: NSNotification) {
      // move back the root view origin to zero
      self.view.frame.origin.y = 0
    }

    @objc func keyboardWillShow(notification: NSNotification) {

        guard let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else {
           // if keyboard size is not available for some reason, dont do anything
           return
        }

      // move the root view up by the distance of keyboard height
      self.view.frame.origin.y = 0 - keyboardSize.height
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        view.endEditing(true)
    }


    @IBAction func validatedPressed(_ sender: AnyObject) {
        if wristFlex.text == "" || wristExtension.text == "" || firstPhalanx.text == "" || secondPhalanx.text == "" || thirdPhalanx.text == "" || forearmLenght.text == "" || handLenght.text == "" || fingerLenght.text == "" || forearmCirecumference.text == "" || wristCirecumference.text == "" || fingerCirecumference.text == "" || handCirecumference.text == "" {
            let alertController = UIAlertController(title: "Erreur", message: " Tous les champs ne sont pas remplis", preferredStyle: .alert)
            let alertAction = UIAlertAction(title: "Ok", style: .default, handler: nil)

            alertController.addAction(alertAction)
            present(alertController, animated: true, completion: nil)

            return
        }
        dismiss(animated: true, completion: nil)
    }
    internal func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        print("")
        return true
    }
}

Upvotes: 1

Views: 847

Answers (2)

JasonJotted
JasonJotted

Reputation: 666

To retrieve what the user writes in the UITextFields, you can access their text properties with something like textField.text.

More specifically in your case something like wristFlex.text, or thirdPhalanx.text can work, and so on, for all of your UITextFields.

Hope this helps someone!

Upvotes: 0

lukas
lukas

Reputation: 214

You can use the textFieldDidEndEditing method from the UITextfieldDelegate. This method gets called when a user is done with his entry. Then you can access variables with textField.text

The example below is an implementation for a case where you have only one textfield. In your case you need to distinguish between the different textfields.

func textFieldDidEndEditing(_ textField: UITextField,
                               reason: UITextField.DidEndEditingReason){
       if let value = textField.text  {
            print(value)
       }
   }

Hope this helps!

Upvotes: 1

Related Questions