循环遍历具有特定标记号的所有 UITextView
Loop through all UITextViews with certain tag number
我有一堆 UITextFields
在自定义 UITableViewCell
中具有相同的标签号。我正在尝试制作一个 for
循环,该循环遍历所有具有特定标签编号的 textFields
。
这是我的代码:
for (UItextField *textField in [cell.contentView viewWithTag:8] {
... // Perform some code
}
当我这样做时,我收到警告:[cell.contentView view...]
说:
Collection expression type 'UIView *' may not respond to 'countByEnumeratingWithState:objects.count'
我可以写什么来代替[cell.contentView.view view...]
?
for (UITextField *textfield in viewThatContainTheTextfields.subviews) {
if (textfield.tag == 8) {
}
}
当您执行 viewWithTag:
时,它将 return 一个单独的 UIView
对象,它不适用于具有相同标签号的视图。所以它将 return 第一个 UIVeiw
它遇到的标签 8
所以我们想要做的是循环遍历该视图中包含的所有子视图。然后检查它是否是 UITextField
并且标记等于 8
。 if statement
的原因是无法保证循环中的对象 return 永远是 UITextField
,即使我们将对象类型从 id
更改为UITextField
仍然不能保证它实际上是 UITextField
所以我们需要 if statement
.
// Retrieve all the subviews from contentView
for (id view in [cell.contentView subviews]) {
// Check that the view is of UITextField and the tag is 8
if ([view isKindOfClass:[UITextField class]]
&& [view tag] == 8) {
// Assign to a UITextField object
UITextField *textField = (UITextField *)view;
// Do whatever we want with that one field
}
}
此代码:[cell.contentView viewWithTag:8]
returns 单个视图,而不是文档中指定的数组:viewWithTag:
如果您想获取所有具有相同标签的视图,一种方法是使用 KVC:
NSArray *filteredViews = [containerView.subviews filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF.tag == %d", 8]]
for (UIView *view in filteredViews) {
if ([view isKindOfClass:[UITextField class]) {
//do something with the view
}
}
我有一堆 UITextFields
在自定义 UITableViewCell
中具有相同的标签号。我正在尝试制作一个 for
循环,该循环遍历所有具有特定标签编号的 textFields
。
这是我的代码:
for (UItextField *textField in [cell.contentView viewWithTag:8] {
... // Perform some code
}
当我这样做时,我收到警告:[cell.contentView view...]
说:
Collection expression type 'UIView *' may not respond to 'countByEnumeratingWithState:objects.count'
我可以写什么来代替[cell.contentView.view view...]
?
for (UITextField *textfield in viewThatContainTheTextfields.subviews) {
if (textfield.tag == 8) {
}
}
当您执行 viewWithTag:
时,它将 return 一个单独的 UIView
对象,它不适用于具有相同标签号的视图。所以它将 return 第一个 UIVeiw
它遇到的标签 8
所以我们想要做的是循环遍历该视图中包含的所有子视图。然后检查它是否是 UITextField
并且标记等于 8
。 if statement
的原因是无法保证循环中的对象 return 永远是 UITextField
,即使我们将对象类型从 id
更改为UITextField
仍然不能保证它实际上是 UITextField
所以我们需要 if statement
.
// Retrieve all the subviews from contentView
for (id view in [cell.contentView subviews]) {
// Check that the view is of UITextField and the tag is 8
if ([view isKindOfClass:[UITextField class]]
&& [view tag] == 8) {
// Assign to a UITextField object
UITextField *textField = (UITextField *)view;
// Do whatever we want with that one field
}
}
此代码:[cell.contentView viewWithTag:8]
returns 单个视图,而不是文档中指定的数组:viewWithTag:
如果您想获取所有具有相同标签的视图,一种方法是使用 KVC:
NSArray *filteredViews = [containerView.subviews filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF.tag == %d", 8]]
for (UIView *view in filteredViews) {
if ([view isKindOfClass:[UITextField class]) {
//do something with the view
}
}