【问题标题】:Loop through subview to check for empty UITextField - Swift循环遍历子视图以检查空的 UITextField - Swift
【发布时间】:2014-09-25 16:53:14
【问题描述】:

我想知道如何从本质上将下面的目标 c 代码转换为 swift。

这将遍历我想要的视图上的所有子视图,检查它们是否是文本字段,然后检查它们是否为空。

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) {
        //...

    }
}

任何帮助都会很棒!

【问题讨论】:

    标签: ios for-loop swift fast-enumeration


    【解决方案1】:

    Swift 2(及更高版本)的更新:从 Swift 2/Xcode 7 开始,这可以简化。

    • 由于 Objective-C “轻量级泛型”,self.view.subviews 在 Swift 中已经被声明为[UIView],因此演员表 不再需要了。
    • 枚举和可选强制转换可以与 for 循环结合使用 带有大小写模式。

    这给出了:

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

    Swift 1.2 的旧答案:

    在 Swift 中,这可以通过 optional downcast 运算符 as?:

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

    见"Downcasting" 在 Swift 书中。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-02-05
        • 2015-11-24
        • 1970-01-01
        • 2014-08-23
        • 2021-10-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多