Red Apple
Red Apple

Reputation: 23

Dynamic textfield creation and access those field in Swift 4

for (index, value) in controlArr.enumerated() {
        print("\(index): \(value)")

        let  yPosition : Int = index+1
        DispatchQueue.main.async {                    

            let categoryField =  UITextField(frame: CGRect(x: 15, y: 84*yPosition+35, width: Int(self.view.frame.width - 35), height: 40))
            // categoryField.placeholder = "Please Enter \(value)"
            categoryField.font = UIFont.systemFont(ofSize: 15)
            categoryField.text  = "\(self.fieldArr[index])"
            categoryField.delegate = self
            categoryField.tag = 100 + index
           // self.myDict["myTextfield\(index)"] = categoryField

            categoryField.borderStyle = UITextBorderStyle.bezel
            categoryField.autocorrectionType = UITextAutocorrectionType.no
            categoryField.keyboardType = UIKeyboardType.default
            categoryField.returnKeyType = UIReturnKeyType.done
            categoryField.clearButtonMode = UITextFieldViewMode.whileEditing;
            categoryField.contentVerticalAlignment = UIControlContentVerticalAlignment.center

            self.arrayOfTextFields.append(categoryField)
            self.scrollView.addSubview(categoryField)

            for item in self.controlArr {

                let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 20))

                if item == "dropdown"
                {
                    categoryField.rightViewMode = UITextFieldViewMode.always
                    imageView.contentMode = .scaleAspectFit
                    let image = UIImage(named:"down.png")
                    imageView.image = image
                    categoryField.rightView = imageView

                }else{

                    imageView.isHidden = true
                }

            }
   }         
  1. How to get the text field like created and used to access those fields.

  2. I tried a lot to achieve in this in Dynamic

Thank you

Upvotes: 0

Views: 2181

Answers (2)

iParesh
iParesh

Reputation: 2368

You can use addTarget method of UITextField and get textfield text changes.

categoryField.addTarget(self, action: #selector(self.textFieldDidChange(_:)), for: .editingChanged)

@objc func textFieldDidChange(_ textField: UITextField) {
        print(textField.text ?? "")
}

Get all textField text

call below function on button click or some where for getting all value of UITextField

func alltextField() {
        for sview in scrollView.subviews {
            if sview is UITextField, let textField = sview as? UITextField {
                print(textField.text ?? "")
            }
        }
    }

Upvotes: 2

Romio Bera
Romio Bera

Reputation: 1

Don't understand why you need this but if you do not want to create text field multiple time in storyboard then you can easily do that taking table view. Inside the table view take a text field and return number as you want and return it (no of rows in table view method). You can easily those text field in (cell for row indexPath).

Upvotes: 0

Related Questions