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

时间:2016-12-01 10:40:56

标签: ios swift textfield

我有一个超过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()
    }
}

8 个答案:

答案 0 :(得分: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!")}

答案 1 :(得分: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 :(得分:2)

试试这个

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

或试试这个

@Rule
public WireMockRule wireMockRule = new WireMockRule(8089);

String response ="Hello world";
StubMapping responseValid = stubFor(get(urlEqualTo(url)).withHeader("Content-Type", equalTo("application/json"))
        .willReturn(aResponse().withStatus(200)
                .withHeader("Content-Type", "application/json").withBody(response)));

答案 3 :(得分:1)

试试这个:)

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

答案 4 :(得分:1)

这对我有用。

var textFieldsArray = [UITextField]()

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

textFieldsArray.forEach { $0.setBottomBorder() }

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

答案 5 :(得分:1)

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

答案 6 :(得分: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"
}

答案 7 :(得分:0)

斯威夫特4和斯威夫特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

        }
    }