0

I am trying to show multiple textFields by running a loop, that whether the number of textfields noTextField is greater than zero (which is an input value), it should display the multiple textFields on ViewController.

Here is what I have one textfield and prints multiple hello but could not display multiple textfields.

What I have

enter image description here

What I want

enter image description here

            if self.noTextFields! > 0 {
            for _ in 0..<self.noTextFields! {
               // self.createForm()
                print ("hello")


                let sampleTextField =  UITextField(frame: CGRect(x: 20, y: 100, width: 300, height: 40))
                sampleTextField.placeholder = "Enter text here"
                sampleTextField.font = UIFont.systemFont(ofSize: 15)
                sampleTextField.borderStyle = UITextBorderStyle.roundedRect
                sampleTextField.autocorrectionType = UITextAutocorrectionType.no
                sampleTextField.keyboardType = UIKeyboardType.default
                sampleTextField.returnKeyType = UIReturnKeyType.done
                sampleTextField.clearButtonMode = UITextFieldViewMode.whileEditing;
                sampleTextField.contentVerticalAlignment = UIControlContentVerticalAlignment.center
                sampleTextField.delegate = self as? UITextFieldDelegate
                self.view.addSubview(sampleTextField)

            }
        }
1
  • All of the text fields are on top of each other as they all have the same frame. You need to adjust your frame y position based on the loop counter Commented Dec 9, 2017 at 10:31

2 Answers 2

1

Mistakes and Corrections:

  1. Check the SubView items contents for the self.view by self.view.items values printing in logs.
  2. You have not assigned the X / Y position for the +1 counter text fields you are allocating to any incremental position so even if the new object will get created it will be overlapped on the same previous item.
  3. I would always recommend to put the textfield object in array at same time when you do addSubview call, immediately after this self.view.addSubview(sampleTextField) call. This will help me to save the reference for each text field item when required to modify / access / deallocate.
  4. You also set the tag for each textfield object so its better to get the object with that tag from the subviews with [self.view viewWithTag:tagNo] call if you dont want an array to be used.

Your snippet will look like

     var xValue = 0,yValue =0
     var tagNo = 0  
     var textObjects = allocate new array.

     if self.noTextFields! > 0 {
                for _ in 0..<self.noTextFields! {
                   // self.createForm()
                    print ("hello")

                    let sampleTextField =  UITextField(frame: CGRect(x: xValue, y: yValue, width: 300, height: 40)) //Note that I have set xValue and yValue
                    sampleTextField.placeholder = "Enter text here"
                    sampleTextField.font = UIFont.systemFont(ofSize: 15)
                    sampleTextField.borderStyle = UITextBorderStyle.roundedRect
                    sampleTextField.autocorrectionType = UITextAutocorrectionType.no
                    sampleTextField.keyboardType = UIKeyboardType.default
                    sampleTextField.returnKeyType = UIReturnKeyType.done
                    sampleTextField.clearButtonMode = UITextFieldViewMode.whileEditing;
                    sampleTextField.contentVerticalAlignment = UIControlContentVerticalAlignment.center
                    sampleTextField.delegate = self as? UITextFieldDelegate
                    sampleTextField.tag = tagNo  //Note this line for tagNo
                    self.view.addSubview(sampleTextField)
                    textObjects.addObject:sampleTextField //Note this line for adding textfield to array for reference to get total items added

yValue = yValue + sampleTextField.frame.size.height + 20; //Added yValue to place another text view at bottom of initial one and 20px buffer for gap.
                }
            }

print "Total Textobjects in array:textObjects.count

If required to get object from view its self.view.viewwithTag(tagNo) will return exact text field.

Sign up to request clarification or add additional context in comments.

2 Comments

thanx for your efforts but I am getting error at textObjects.addObject:sampleTextField
Have you allocated/init the array also try printing the the text field and check if it’s allocated properly
0

You just add dynamic value of y(add x if required).

var counter = 30
var yValue = 0

in for loop you just need to add below code

let sampleTextField =  UITextField(frame: CGRect(x: 20, y: yValue, width: 300, height: 40))

yValue = Counter
//(space each textfield if 30 because height of textfield is 40)

counter = counter + 70

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.