How to get all the textfields from a view in swift

假如想象 提交于 2019-11-29 10:01:47

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!")}

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()
    }
}

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()
}
}

Try this :)

for view in self.view.subviews as! [UIView] {
    if let textField = view as? UITextField {
        textField.setBottomBorder()
    }
}

This worked for me.

var textFieldsArray = [UITextField]()

for view in self.view.subviews {
    if view is UITextField {
        textFieldsArray.append(view as! UITextField)
    }
}

textFieldsArray.forEach { $0.setBottomBorder() }

If you want to get the result of the function applied in a new array, use map() instead.

func getTextFields() {
    for textField in view.subviews where view is UITextField {
       (textField as? UITextField).setBottomBorder()
    }
}

extension:

extension UIView {
    func viewOfType<T:UIView>(type:T.Type, process: (_ view:T) -> Void)
    {        
        if let view = self as? T
        {
            process(view)
        }
        else {
            for subView in subviews
            {
                subView.viewOfType(type:type, process:process)
            }
        }
    }
}

Usage:

view.viewOfType(type:UITextField.self) {
        view in

        view.text = "123"
}

Swift 4 and Swift 3: -
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

        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!