ios 循环遍历子视图以检查空的 UITextField - Swift
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25097958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loop through subview to check for empty UITextField - Swift
提问by Ryan
I"m wondering how to essentially transform the objective c code below into swift.
我想知道如何从本质上将下面的目标 c 代码转换为 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!
任何帮助都会很棒!
采纳答案by Shakeel Ahmed
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.
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
}
}
}
}
回答by Martin R
Update for Swift 2 (and later):As of Swift 2/Xcode 7 this can be simplified.
Swift 2(及更高版本)的更新:从 Swift 2/Xcode 7 开始,这可以简化。
- 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.
- 由于 Objective-C 的“轻量级泛型”
self.view.subviews
已经[UIView]
在 Swift 中声明,因此不再需要强制转换。 - 枚举和可选的强制转换可以与具有 case-pattern 的 for 循环结合使用。
This gives:
这给出:
for case let textField as UITextField in self.view.subviews {
if textField.text == "" {
// show error
return
}
}
Old answer for Swift 1.2:
Swift 1.2 的旧答案:
In Swift this is nicely done with the optional downcastoperator as?
:
在 Swift 中,这可以通过可选的向下转换运算符很好地完成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.
请参阅 Swift 书中的“向下转型”。