如何从swift视图中获取所有文本字段

问题描述 投票:9回答:8

我有一个超过15 UITextFields的视图。我必须为所有UITextFields设置bottomBorder(扩展名)。我可以为所有UITextFields和它的工作一个接一个地设置它。我想立刻为所有UITextFields设置底部边框。这是我正在尝试的代码,但似乎for循环没有执行。我甚至在viewDidLayoutSubViews尝试过但是循环没有在那里执行。

 override func viewDidLoad() 
{
    super.viewDidLoad()

    /** setting bottom border of textfield**/

    for case let textField as UITextField in self.view.subviews {
        textField.setBottomBorder()
    }
}
ios swift textfield
8个回答
13
投票

Swift:此函数将返回视图中的所有文本字段。无论任何子视图中是否存在字段。 ;-)

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

用法:

_=getAllTextFields(fromView : self.view).map{($0.text = "Hey dude!")}

8
投票

我使它工作,但仍然需要解释为什么有问题的代码不起作用 我从论坛的某个地方得到它,而不是完全可以归功于答案。

/** 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  

在viewDidLoad或viewDidLayoutSubviews中调用上面的函数。

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

/** setting bottom border to the textfield **/
    let allTextField = getTextfield(view: self.view)
    for txtField in allTextField
    {
        txtField.setBottomBorder()
    }
}

2
投票

试试这个

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

或试试这个

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

1
投票

试试这个 :)

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

1
投票

这对我有用。

var textFieldsArray = [UITextField]()

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

textFieldsArray.forEach { $0.setBottomBorder() }

如果要获取在新数组中应用的函数的结果,请改用map()。


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

1
投票

延期:

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

用法:

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

        view.text = "123"
}

0
投票

Swift 4和Swift 3: - 一个非常简单的答案你可以很容易理解: - 你可以处理所有类型的对象,如UILable,UITextfields,UIButtons,UIView,UIImages。任何形式的目标等

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

        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.