iPhone:如何通过UITextField枚举 [英] iPhone: How to enumerate through UITextFields

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

问题描述

这里的简单问题:通过子视图枚举来查找文本字段的正确方法是什么?

Simple question here: 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.

有关详细信息,请参阅这个问题

For more information, see this question.


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

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

这就是类型转换的目的。

This is what typecasting is for.

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

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

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