我有一个超过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()
}
}
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!")}
我使它工作,但仍然需要解释为什么有问题的代码不起作用 我从论坛的某个地方得到它,而不是完全可以归功于答案。
/** 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()
}
}
试试这个
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()
}
}
试试这个 :)
for view in self.view.subviews as! [UIView] {
if let textField = view as? UITextField {
textField.setBottomBorder()
}
}
这对我有用。
var textFieldsArray = [UITextField]()
for view in self.view.subviews {
if view is UITextField {
textFieldsArray.append(view as! UITextField)
}
}
textFieldsArray.forEach { $0.setBottomBorder() }
如果要获取在新数组中应用的函数的结果,请改用map()。
func getTextFields() {
for textField in view.subviews where view is UITextField {
(textField as? UITextField).setBottomBorder()
}
}
延期:
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"
}
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
}
}