Loop through subview to check for empty UITextField - Swift

隐身守侯 提交于 2019-11-26 19:52:02

问题


I"m wondering how to essentially transform the objective c code below into swift.

This will loop through all the subviews on my desired view, check if they are textfields, and then check if they are empty of not.

for (UIView *view in contentVw.subviews) {
    NSLog(@"%@", view);
    if ([view isKindOfClass:[UITextField class]]) {
        UITextField *textfield = (UITextField *)view;
        if (([textfield.text isEqualToString:""])) {
            //show error
            return;
        }
    }
}

Here is where i am with swift translation so far:

for view in self.view.subviews as [UIView] {
    if view.isKindOfClass(UITextField) {
        //...

    }
}

Any help would be great!


回答1:


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



回答2:


Update for Swift 2 (and later): As of Swift 2/Xcode 7 this can be simplified.

  • Due to the Objective-C "lightweight generics", self.view.subviews is already declared as [UIView] in Swift, therefore the cast is not necessary anymore.
  • Enumeration and optional cast can be combined with to a for-loop with a case-pattern.

This gives:

for case let textField as UITextField in self.view.subviews {
    if textField.text == "" {
        // show error
        return
    }
}

Old answer for Swift 1.2:

In Swift this is nicely done with the optional downcast operator as?:

for view in self.view.subviews as! [UIView] {
    if let textField = view as? UITextField {
        if textField.text == "" {
            // show error
            return
        }
    }
}

See "Downcasting" in the Swift book.



来源:https://stackoverflow.com/questions/25097958/loop-through-subview-to-check-for-empty-uitextfield-swift

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