遍历子视图以检查是否为空的UITextField-Swift
我想知道如何从本质上将下面的目标c代码转换为快速代码。
这将遍历我所需视图上的所有子视图,检查它们是否为文本字段,然后检查是否为空。
for (UIView *view in contentVw.subviews) {    NSLog(@"%@", view);
    if ([view isKindOfClass:[UITextField class]]) {
        UITextField *textfield = (UITextField *)view;
        if (([textfield.text isEqualToString:""])) {
            //show error
            return;
        }
    }
}
到目前为止,这里是我快速翻译的地方:
for view in self.view.subviews as [UIView] {    if view.isKindOfClass(UITextField) {
        //...
    }
}
任何帮助将是巨大的!
回答:
Swift 5和Swift
4:-一个非常简单的答案,您可以轻松理解:-您可以处理各种对象,例如UILable,UITextfields,UIButtons,UIView,UIImages。任何种类的对象等
for subview in self.view.subviews{
    if subview is UITextField
    {
        //MARK: - if the sub view is UITextField you can handle here
        if subview.text == ""
        {
            //MARK:- Handle your code
        }
    }
    if subview is UIImageView
    {
     //MARK: - check image
      if subview.image == nil
      {
             //Show or use your code here
      }
    }
}
//MARK:- You can use it any where, where you need it
//Suppose i need it in didload function we can use it and work it what do you need
override func viewDidLoad() {
    super.viewDidLoad()
    for subview in self.view.subviews
    {
        if subview is UITextField
        {
         //MARK: - if the sub view is UITextField you can handle here
            if subview.text == ""
            {
               //MARK:- Handle your code
            }
        }
        if subview is UIImageView
        {
          //MARK: - check image
            if subview.image == nil
                {
                 //Show or use your code here
                }
            }
        }
    }
以上是 遍历子视图以检查是否为空的UITextField-Swift 的全部内容, 来源链接: utcz.com/qa/406425.html








