如何在iOS上通过UITextFields枚举 [英] How to enumerate through UITextFields on iOS

查看:55
本文介绍了如何在iOS上通过UITextFields枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过子视图枚举以查找文本字段的正确方法是什么?

Which is the correct way of enumerating through sub views to find text fields?

NSMutableArray *mutableTFs = [[NSMutableArray alloc] init];

for (UIView *view in [self.view subviews]) {
    if ([view isKindOfClass:[UITextField class]]) {            
        [mutableTFs addObject:view];
    }
}

OR

NSMutableArray *mutableTFs = [[NSMutableArray alloc] init];

for (UITextField *textField in [self.view subviews]) {
    [mutableTFs addObject:textField];
}

我知道这不是正确的措词,但是我不明白这是否是最主要的方法,如何将其从视图转换"为文本字段?

I know this isn't the correct wording, but what I don't understand is if it is the top method, how do you 'convert' it from a view to a text field?

推荐答案

哪种是通过子视图枚举来查找文本的正确方法字段?

Which is the correct way of enumerating through sub views to find text fields?

第一种方法是正确的.第二种方法将遍历所有子视图,而不仅仅是类型为 UITextField 的子视图. for()中的类型只是对编译器的提示.

The first method is the correct one. The second method will iterate over all the subviews, not just the subviews with type UITextField. The type in the for() is only a hint to the compiler.

有关更多信息,请参见此问题

如何将其从视图转换"为文本字段?

how do you 'convert' it from a view to a text field?

这是类型转换的目的.

for (UIView *view in [self.view subviews]) {
    if ([view isKindOfClass:[UITextField class]]) {
        // you don't need to cast just to add to the array         
        [mutableTFs addObject:view];
        // typecasting works as it does in C
        UITextField *textField = (UITextField *)view;
        // do something with textField
    }
}

这篇关于如何在iOS上通过UITextFields枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆