1
 for subviews in self.profilescroll.subviews
    {
        if ((subviews is UITextField) && (subviews.tag == 0))
        {


            let btntext : UITextField = subviews as! UITextField
            btntext.resignFirstResponder()

        }

        if ((subviews is UITextField) && (subviews.tag == 1))
        {
            let btntext : UITextField = subviews as! UITextField
            btntext.resignFirstResponder()
        }
    }

this is where i put tag to the textfield.

 for i in 0 ..< 2
                    {
                        btnbudget = UITextField()
                        btnbudget.frame = CGRect(x: (CGFloat(i) * (((screenWidth - 160)/2) + 40)) + 40, y: 0, width: (screenWidth - 160)/2, height: 50)
                        btnbudget.font = UIFont(name: "Metropolis-SemiBold", size: 18)
                        btnbudget.layer.cornerRadius = 10.0
                        btnbudget.layer.borderWidth = 1.0
                        btnbudget.layer.borderColor = UIColor(hexString:"4B45A0").cgColor
                        btnbudget.tag = i
                        btnbudget.backgroundColor = UIColor(hexString: "#00000000")
                        btnbudget.text = budgetlist.object(at: i) as? String
                        btnbudget.textColor = UIColor(hexString:"4B45A0")
                        btnbudget.textAlignment = .center
                        btnbudget.delegate = self
                        budgetbtnview.addSubview(btnbudget)
}

where profilescroll is the view with 3 textfields in which i need to find textfield with tag values ranging from 0 to 1. but for some reason i am not getting the right one hence the keyboard is not returning.

vishnu
  • 213
  • 1
  • 13
  • 1
    Can you show the code where you set the tag for the textfields' ? – Rakesha Shastri Jul 10 '18 at 06:57
  • Are the text fields the immediate children of `profileScroll`? Is `profileScroll` a `UIScrollView` with a content view? – Chris Jul 10 '18 at 07:01
  • By default zero for every components. Did you set tag anywhere explicitly to your text field? where you are adding this budgetbtnview to scrollview – karthikeyan Jul 10 '18 at 07:01
  • code edited yes i have explicitly set tags – vishnu Jul 10 '18 at 07:03
  • You are discouraged from setting a zero tag explicitly because that's the default. Start with 10 or 100. And this is Swift: You can omit all parentheses around the `if` conditions: `if subviews is UITextField && subviews.tag == 0 { ...` is pretty valid syntax. And the plural form `subviews` for an actual **single** element is a bit confusing. – vadian Jul 10 '18 at 07:06
  • When you just want to dismiss the keyboard, why not simply use `self.view.endEditing(true)` ? – Andreas Oetjen Jul 10 '18 at 07:06
  • If using a Storyboard you could simply set an outlet to the text field. – Chris Jul 10 '18 at 07:08

3 Answers3

4

You have assigned tag number to text field than don't need to loop over all subviews.

  textField.tag = 101

You can directly use tag number to get view like below...

if let txtField = self.view.viewWithTag(101) as? UITextField {
    print(txtField.text)

}

Make sure all the tag you give to view should be unique otherwise you may get unwanted view.

Mahendra
  • 8,448
  • 3
  • 33
  • 56
1

given tag should be unique ...try this..

 for i in 100 ..< 102
{
btnbudget = UITextField()
btnbudget.frame = CGRect(x: (CGFloat(i) * (((screenWidth - 160)/2) + 40)) + 40, y: 0, width: (screenWidth - 160)/2, height: 50)
btnbudget.font = UIFont(name: "Metropolis-SemiBold", size: 18)
btnbudget.layer.cornerRadius = 10.0
btnbudget.layer.borderWidth = 1.0
btnbudget.layer.borderColor = UIColor(hexString:"4B45A0").cgColor
btnbudget.tag = i
btnbudget.backgroundColor = UIColor(hexString: "#00000000")
btnbudget.text = budgetlist.object(at: i) as? String
btnbudget.textColor = UIColor(hexString:"4B45A0")
btnbudget.textAlignment = .center
btnbudget.delegate = self
budgetbtnview.addSubview(btnbudget)
}

and ....

for subviews in self.profilescroll.subviews
{
if ((subviews is UITextField) && (subviews.tag == 100))
{


let btntext : UITextField = subviews as! UITextField
btntext.resignFirstResponder()

}

if ((subviews is UITextField) && (subviews.tag == 101))
{
let btntext : UITextField = subviews as! UITextField
btntext.resignFirstResponder()
}
}

but self.view.viewWithTag(101) approach is more appropriate than this

Deepti Raghav
  • 852
  • 1
  • 9
  • 26
1

Let say you have three UITextField and you have set the tags as 0,1,2

Now, you can simply loop through all the Subview of your profilescroll

for case let textField as UITextField in self.profilescroll.subviews {
    if textField.tag == 0 {
        // do something
        return
    }
    else if textfield.tag == 1 {
      // do something 
      return
    }
    else if textfield.tag == 2 {
    // do something 
    return
    }
}

This will also ignore cases where your subview is not an UITextField thereby saving time.

Avinash Sharma
  • 665
  • 1
  • 7
  • 23