How to get all the textfields from a view in swift

前端 未结 8 848
予麋鹿
予麋鹿 2020-12-17 17:53

I have a view which has more than 15 UITextFields. I have to set bottomBorder(extension) for all the UITextFields. I can set it one by one for all

相关标签:
8条回答
  • 2020-12-17 18:17
    func getTextFields() {
        for textField in view.subviews where view is UITextField {
           (textField as? UITextField).setBottomBorder()
        }
    }
    
    0 讨论(0)
  • 2020-12-17 18:20

    I made it working, but still need the explanation why the code in question is not working
    I got it from somewhere on the forum, not exactle able to credit the answer.

    /** extract all the textfield from view **/
    func getTextfield(view: UIView) -> [UITextField] {
    var results = [UITextField]()
    for subview in view.subviews as [UIView] {
        if let textField = subview as? UITextField {
            results += [textField]
        } else {
            results += getTextfield(view: subview)
        }
    }
    return results  
    

    Call the above function in viewDidLoad or viewDidLayoutSubviews.

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
    
    /** setting bottom border to the textfield **/
        let allTextField = getTextfield(view: self.view)
        for txtField in allTextField
        {
            txtField.setBottomBorder()
        }
    }
    
    0 讨论(0)
  • 2020-12-17 18:20

    Try this :)

    for view in self.view.subviews as! [UIView] {
        if let textField = view as? UITextField {
            textField.setBottomBorder()
        }
    }
    
    0 讨论(0)
  • 2020-12-17 18:22

    try this

    for aSubView: Any in self.view.subviews {
    if (aSubView is UITextField) {
        var textField = (aSubView as! UITextField)
        textField. setBottomBorder()
    }
    }     
    

    or try this

    for view in self.view.subviews {
    if (view is UITextField) {
         var textField = view as! UITextField
          textField. setBottomBorder()
    }
    }
    
    0 讨论(0)
  • 2020-12-17 18:26

    Swift: This function will return all text-fields in a view. No matter if field exists in any subview. ;-)

    func getAllTextFields(fromView view: UIView)-> [UITextField] {
        return view.subviews.flatMap { (view) -> [UITextField]? in
            if view is UITextField {
                return [(view as! UITextField)]
            } else {
                return getAllTextFields(fromView: view)
            }
        }.flatMap({$0})
    }
    

    Usage:

    _=getAllTextFields(fromView : self.view).map{($0.text = "Hey dude!")}
    
    0 讨论(0)
  • 2020-12-17 18:39

    Swift 5
    A Very simple answer you can understand easyly : - You can handle all kind of Objects like UILable, UITextfields, UIButtons, UIView, UIImages . any kind of objecs etc.

    for subviews in self.view.subviews {
        if subviews is UITextField
        {
            //MARK: - if the sub view is UITextField you can handle here
            funtextfieldsetting(textfield: subviews as! UITextField)
        }
        if subviews is UIButton
        {
            //MARK: - if the sub view is UIButton you can handle here
            funbuttonsetting(button: subviews as! UIButton)
        }
        if subviews is UILabel
        {
            //MARK: - if the sub view is UILabel you can handle here
            //Any thing you can do it with label or textfield etc
               
         }
     }
    
    0 讨论(0)
提交回复
热议问题