iosswiftuitextfieldxcode9.2

unable to get proper textfield with tag while looping through uitextfield class in swift?


 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.


Solution

  • 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