我有一个UIView类,它有许多UITextField子类的子视图。有没有办法迭代一个类列表来找到UITextField子类
答案 0 :(得分:2)
是的!
假设您将代码放在UIView子类中,您可以获取一组子视图......
NSArray *sbviews = [self subviews];
NSMutableArray *textFields; //placeholder for your UITextField subclassed objects.
//enumerate through the subview collection and only add objects to the textFields array that are UITextField objects.
for (id anObject in sbviews) {
if([anObject isKindOfClass: [UITextField class]]){
[textField addObject: anObject];
}
}
textField数组现在将包含UITextField类的所有对象...
答案 1 :(得分:0)
您的UIView类有一个子视图数组,列出了您添加到它的所有子视图。您可以通过数组进行迭代,并查找UITextField对象的实例(而不是类)。
for (UITextField* textField in [someView subviews]) {
if ([textField isKindOfClass:[UITextField class]]) {
// found one, textField really is a UITextField
}
}